0

I wan to implement a timeout on the child spawned by a subprocess call.

So as a test:

#!/home/depot/.conda/envs/python3/bin/python
import subprocess                                                     
if __name__ == "__main__":
    subprocess.call('./sleep.py', timeout=1)

The sleep.py file being run:

#!/home/depot/.conda/envs/python36/bin/python
from time import sleep

if __name__ =="__main__":
     for j in range(3):
         print(j)
         sleep(1)

Now clearly this doesn't implement any timeout.

I was a bit surprised as the doc seems to suggest that timeout is indeed an option in:
Using the subprocess Module.

However other answers on SO seems to disregard that functionality and advocate for the creation of a custom made timer:
Subprocess timeout does not work
Using module 'subprocess' with timeout

How is it possible to implement a timeout with the builtin timeout argument?

martineau
  • 119,623
  • 25
  • 170
  • 301
jim jarnac
  • 4,804
  • 11
  • 51
  • 88
  • Works as expected for me. After 1 second I get a `subprocess.TimeoutExpired` exception and sleep.py is killed. What happens for you? What version of python are you using? (I have tested on 3.4 and 3.5 - both work). What OS are you using? – Gavin Jun 01 '17 at 23:52
  • @Gavin In my case the script sleep.py just continue until the end without stopping after the timeout. I am on Linux, using python 3.6 (I also tried with 3.4 with similar results). I added the shebang at the start of the script. This is the actual way I am calling them - maybe that does make a difference? – jim jarnac Jun 01 '17 at 23:58
  • Hm are the shebangs supposed to be different? I tend to use `#!/usr/bin/env python` so that it grabs the first python on the path (and let virtualenv manage it), so perhaps something is happening with the multiple environment setup. – Gavin Jun 02 '17 at 00:40

0 Answers0