How to timeout when executing command line programs in Python?

前端 未结 3 1456
南笙
南笙 2021-01-25 21:21

I am executing Maple from Python and would like to stop the program if it exceeds a maximum time. If its a Python function this can be done by using a timeout-decorator. But I a

3条回答
  •  深忆病人
    2021-01-25 21:48

    You can use subprocess.Popen to spawn a child process. Make sure to handle stdout and stderr properly. Then use Popen.wait(timeout) call and kill the process when TimeoutExpired arrive.

提交回复
热议问题