Python subprocess timeout?

后端 未结 10 2135
旧时难觅i
旧时难觅i 2020-12-09 04:06

Is there any argument or options to setup a timeout for Python\'s subprocess.Popen method?

Something like this:

subprocess.Popen([\'..\'], ..., timeout

10条回答
  •  醉话见心
    2020-12-09 04:28

    No there is no time out. I guess, what you are looking for is to kill the sub process after some time. Since you are able to signal the subprocess, you should be able to kill it too.

    generic approach to sending a signal to subprocess:

    proc = subprocess.Popen([command])
    time.sleep(1)
    print 'signaling child'
    sys.stdout.flush()
    os.kill(proc.pid, signal.SIGUSR1)
    

    You could use this mechanism to terminate after a time out period.

提交回复
热议问题