Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Popen.poll() return a return code of None even though the sub-process has completed?
    text
    copied!<p>I have some Python code that runs on Windows that spawns a subprocess and waits for it to complete. The subprocess isn't well behaved so the script makes a non-blocking spawn call and watches the process on the side. If some timeout threshold is met it kills of the process, assuming it has gone of the rails.</p> <p>In some instances, which are non-reproducible, the spawned subprocess will just disappear and the watcher routine won't pick up on this fact. It'll keep watching until the timeout threshold is passed, try to kill the subprocess and get an error, and then exit.</p> <p><strong>What might be causing the fact that the subprocess has gone away to be undetectable to the watcher process? Why isn't the return code trapped and returned by the call to <code>Popen.poll()</code>?</strong></p> <p>The code I use to spawn and watch the process follows:</p> <pre><code>import subprocess import time def nonblocking_subprocess_call(cmdline): print 'Calling: %s' % (' '.join(cmdline)) p = subprocess.Popen(cmdline, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) return p def monitor_subprocess(handle, timeout=1200): start_time = time.time() return_code = 0 while True: time.sleep(60) return_code = handle.poll() if return_code == None: # The process is still running. if time.time() - start_time &gt; timeout: print 'Timeout (%d seconds) exceeded -- killing process %i' % (timeout, handle.pid) return_code = handle.terminate() # give the kill command a few seconds to work time.sleep(5) if not return_code: print 'Error: Failed to kill subprocess %i -- return code was: %s' % (handle.pid, str(return_code)) # Raise an error to indicate that the process was hung up # and we had to kill it. raise RuntimeError else: print 'Process exited with return code: %i' % (return_code) break return return_code </code></pre> <p>What I'm seeing is that, in cases where the process has disappeared, the call to <code>return_code = handle.poll()</code> on line 15 is returning <code>None</code> instead of a return code. I know the process has gone away completely -- I can see that it is no longer there in Task Manager. And I know the process disappeared long before the timeout value was reached.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload