Reputation: 48
If using subprocess to execute an xterm on linux, which in turn executes some other process, it seems that Python (2.6.5) will never recognize that the process (xterm) has completed execution.
Consider the following code:
import subprocess
import shlex
import time
proc = subprocess.Popen(shlex.split('xterm -iconic -title "FOO_BAR" -e sleep 5'))
while True:
if proc.poll():
print 'Process completed'
time.sleep(0.1)
This will loop infinitely until you terminate the Python interpreter. I'm guessing that this is probably caused by some oddity with xterm, and not a direct cause of the Python subprocess module, but maybe there are some other smart people out there that could shed some light on the situation.
Note: Calling proc.communicate()
will in fact return when the xterm completes, but for some reason the poll method will not work.
Upvotes: 2
Views: 1362
Reputation: 5828
This program doesn't distinguish between proc.poll()
returning None
(meaning that the process is still running) and proc.poll()
returning 0
(meaning that the process terminated with an exit value of zero, conventionally indicating successful completion). Change this line:
if proc.poll():
to this:
if proc.poll() is not None:
and see if that helps.
Upvotes: 3