Python has a great standard library when it comes to invoking external processes. But one weakness it does have is that it’s not easy to communicate with a subprocess while it’s running, i.e. streaming its stdout. If you look at the documentation for
popen, you will repeatedly see caveats like the following from the Python docs for Popen.communicate:
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.
It’s actually a well known problem. There is even an open enhancement proposal PEP 3145 to address it. But that is currently on track for Python 3.2. Python 2.x will never get that update.
Hunting around, I found a pretty decent partial solution on Stackoverflow, but it took me quite a bit to tweaking to get it to work in my case.
As you can see, this code is invoking the
mysql command-line client in batch mode. I’m piping a file into my python script, and then turning around and piping that into mysql. I start the mysql subprocess, but at the same time I’m spinning off a worker thread to read its output. Additionally, I’m re-opening stdout in non-blocking mode, so I don’t have to wait for a buffer to fill up before I can read a chunk.
Then I’m reading the mysql output and writing it both to the console, and to a log file. For this application, it’s critical that the mysql output be shown on the screen as it’s running. What if there’s an exception; the user will want to terminate before it runs any further.
Doesn’t mysql provide logging by default? Yes, but only for interactive (i.e., non-batch) sessions. From the mysql command-line tool docs:
By using the –tee option when you invoke mysql, you can log statements and their output. All the data displayed on the screen is appended into a given file. This can be very useful for debugging purposes also. mysql flushes results to the file after each statement, just before it prints its next prompt. Tee functionality works only in interactive mode.
One additional caveat; you need to make sure that the subprocess you are invoking is not doing its own buffering. It took me a bit to figure out that mysql does do that, which is what the
--unbuffered flag is there to disable.