Skip to content Skip to sidebar Skip to footer

Python Printing Stdout As It Received

I'm trying to run wrap a simple (windows) command line tool up in a PyQt GUI app that I am writing. The problem I have is that the command line tool throws it's progress out to st

Solution 1:

Interactive communication through stdin/stdout is a common problem.

You're in luck though, with PyQt you can use QProcess, as described here: http://diotavelli.net/PyQtWiki/Capturing_Output_from_a_Process

Solution 2:

Do I understand the question? I believe you're running something like "echo first; sleep 60; echo second" and you want see the "first" well-ahead of the "second", but they're both spitting out at the same time.

The reason you're having issues is that the operating system stores the output of processes in its memory. It will only take the trouble of sending the output to your program if the buffer has filled, or the other program has ended. So, we need to dig into the O/S and figure out how to tell it "Hey, gimme that!" This is generally known as asynchronous or non-blocking mode.

Luckily someone has done the hard work for us. This guy has added a send() and recv() method to the python built-in Popen class. It also looks like he fixed the bugs that people found in the comments.

Try it out: http://code.activestate.com/recipes/440554/

Post a Comment for "Python Printing Stdout As It Received"