How do I get 'real-time' information back from a subprocess.Popen in python (2.5)?

Update with code that appears not to work (on windows anyway).

Update with code that appears not to work (on windows anyway) class ThreadWorker(threading. Thread): def __init__(self, callable, *args, **kwargs): super(ThreadWorker, self). __init__() self.

Callable = callable self. Args = args self. Kwargs = kwargs self.

SetDaemon(True) def run(self): try: self. Callable(*self. Args, **self.

Kwargs) except wx. PyDeadObjectError: pass except Exception, e: print e if __name__ == "__main__": import os from subprocess import Popen, PIPE def worker(pipe): while True: line = pipe.readline() if line == '': break else: print line proc = Popen("python subprocess_test. Py", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout_worker = ThreadWorker(worker, proc.

Stdout) stderr_worker = ThreadWorker(worker, proc. Stderr) stdout_worker.start() stderr_worker.start() while True: pass.

This is clearly the best answer. Thanks for showing me this 'pipe' type in Python! – Mapad Nov 26 '09 at 10:11 This is a great answer, and works for me.

The key is that the reads can block without any problem due to the threads. – Paul Biggar Dec 4 '10 at 21:04.

Stdout will be buffered - so you won't get anything till that buffer is filled, or the subprocess exits. You can try flushing stdout from the sub-process, or using stderr, or changing stdout on non-buffered mode.

It sounds like the issue might be the use of buffered output by the subprocess - if a relatively small amount of output is created, it could be buffered until the subprocess exits. Some background can be found here.

Here's what worked for me: cmd = ". /tester_script. Bash" p = subprocess.

Popen( cmd, shell=False, stdout=subprocess. PIPE, stderr=subprocess. PIPE ) while p.poll() is None: out = p.stdout.readline() do_something_with( out, err ) In your case you could try to pass a reference to the sub-process to your Worker Thread, and do the polling inside the thread.

I don't know how it will behave when two threads poll (and interact with) the same subprocess, but it may work. Also note thate the while p.poll() is None: is intended as is. Do not replace it with while not p.poll() as in python 0 (the returncode for successful termination) is also considered False.

You might try this StackOverflow thread. If that's not it, then try this StackOverflow search.

I've been running into this problem as well. The problem occurs because you are trying to read stderr as well. If there are no errors, then trying to read from stderr would block.

On Windows, there is no easy way to poll() file descriptors (only Winsock sockets). So a solution is not to try and read from stderr.

I've used the pexpect module for this, it seems to work ok. sourceforge.net/projects/pexpect.

Using pexpect noah.org/wiki/Pexpect with non-blocking readlines will resolve this problem. It stems from the fact that pipes are buffered, and so your app's output is getting buffered by the pipe, therefore you can't get to that output until the buffer fills or the process dies.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions