1

Is it possible for Popen.communicate(timeout=2) to raise TimeoutExpired, even though I am asserting that Popen.poll() is not None and Popen.wait(2) does not throw an exception?

Edit: The docs suggest to use the following snippet:

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

but this will just raise ProcessLookupError: [Errno 3] No such process. Which makes sense because I've already terminated the process via poll and wait.

user2084795
  • 704
  • 1
  • 7
  • 20

1 Answers1

1

Yes. It is possible if the child spawns its own subprocesses. Popen.communicate() may return much later than Popen.wait() which waits only for the child. See Python subprocess' check_call() vs check_output().

Note: proc.kill() might not kill the whole process tree. See How to terminate a python subprocess launched with shell=True.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670