tags:

views:

41

answers:

1

Hi. I am trying to write a small app that uses the subprocess module.

My program calls an external Bash command that takes some time to process. During this time, I would like to show the user a series of messages like this:

Processing. Please wait...
The output is foo()

How can I do this using Popen.wait() or Popen.poll(). I have read that I need to use the Popen.returncode, but how I can get it to actively check the state, I don't know.

+1  A: 

Both wait() and poll() return None if the process has not yet finished, and something different if the process has finished (I think an integer, the exit code, hopefully 0).

So I think you should do something like:

while myprocess.poll() is None:
    print "Still working"
    sleep a while

Be aware that if the bash script creates a lot of output you must use communicate() or something similar to prevent stdout or stderr to become stuffed.

extraneon
I am using communicate(). But I don't get it as to how I can return the returncode while the process is being executed.
sukhbir
I solved it by doing this: if process.poll() is None: print 'Working'
sukhbir