Both wait()
(with timeout
specified) 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).
Edit:
wait()
and poll()
have different behaviors:
wait
(without the timeout argument) will block and wait for the process to complete.wait
with the timeout argument will waittimeout
seconds for the process to complete. If it doesn’t complete, it will throw theTimeoutExpired
exception. If you catch the exception, you’re then welcome to go on, or towait
again.poll
always returns immediately. It effectively does a wait with a timeout of 0, catches any exception, and returnsNone
if the process hasn’t completed.- With either
wait
orpoll
, if the process has completed, the popen object’sreturncode
will be set (otherwise it’s None – you can check for that as easily as callingwait
orpoll
), and the return value from the function will also be the process’s return code.
</Edit>
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.