Python子进程超时?
是否有任何参数或选项可以为 Python 的 subprocess.Popen 方法设置超时?
像这样的东西:
subprocess.Popen(['..'], ..., timeout=20)
?
Is there any argument or options to setup a timeout for Python's subprocess.Popen method?
Something like this:
subprocess.Popen(['..'], ..., timeout=20)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我建议查看
Timer
threading
模块中的 class。我用它来实现Popen
的超时。首先,创建一个回调:
然后打开进程:
然后创建一个将调用回调的计时器,并将进程传递给它。
在程序稍后的某个位置,您可能需要添加以下行:
否则,python 程序将继续运行,直到计时器完成运行。
编辑:我被告知存在竞争条件,
子进程
p
可能在p.poll()
和p之间终止.kill() 调用。我相信下面的代码可以解决这个问题:
尽管您可能想要清理异常处理以专门处理子进程已经正常终止时发生的特定异常。
I would advise taking a look at the
Timer
class in thethreading
module. I used it to implement a timeout for aPopen
.First, create a callback:
Then open the process:
Then create a timer that will call the callback, passing the process to it.
Somewhere later in the program, you may want to add the line:
Otherwise, the python program will keep running until the timer has finished running.
EDIT: I was advised that there is a race condition that the
subprocess
p
may terminate between thep.poll()
andp.kill()
calls. I believe the following code can fix that:Though you may want to clean the exception handling to specifically handle just the particular exception that occurs when the subprocess has already terminated normally.
subprocess.Popen 不会阻塞,因此您可以执行以下操作:
它有一个缺点,您必须始终等待至少 20 秒才能完成。
subprocess.Popen doesn't block so you can do something like this:
It has a drawback in that you must always wait at least 20 seconds for it to finish.
其输出应该是:
可以看出,在第一次执行中,进程正确完成(返回代码 0),而在第二次执行中,进程被终止(返回代码 -15)。
我没有在windows下测试过;但是,除了更新示例命令之外,我认为它应该可以工作,因为我在文档中没有找到任何表明不支持 thread.join 或 process.terminate 的内容。
The output of this should be:
where it can be seen that, in the first execution, the process finished correctly (return code 0), while the in the second one the process was terminated (return code -15).
I haven't tested in windows; but, aside from updating the example command, I think it should work since I haven't found in the documentation anything that says that thread.join or process.terminate is not supported.
您可以
使用 Twisted 的异步流程 API。
You could do
using Twisted's asynchronous process API.
没有内置 python 子进程自动超时功能,因此您必须构建自己的子进程。
这对我在运行 python 2.7.3 的 Ubuntu 12.10 上有效
。将其放入名为 test 的文件中。 py
保存并运行:
sleep 20
命令需要 20 秒才能完成。如果 3 秒内没有终止(不会),则进程将终止。进程运行与终止之间有三秒钟的时间。
A python subprocess auto-timeout is not built in, so you're going to have to build your own.
This works for me on Ubuntu 12.10 running python 2.7.3
Put this in a file called test.py
Save it, and run it:
The
sleep 20
command takes 20 seconds to complete. If it doesn't terminate in 3 seconds (it won't) then the process is terminated.There is three seconds between when the process is run, and it is terminated.
从 Python 3.3 开始,子进程模块中的阻塞辅助函数还有一个
timeout
参数。https://docs.python.org/3/library/subprocess.html
As of Python 3.3, there is also a
timeout
argument to the blocking helper functions in the subprocess module.https://docs.python.org/3/library/subprocess.html
不幸的是,没有这样的解决方案。我设法使用线程计时器来做到这一点,该计时器将与超时后杀死它的进程一起启动,但由于僵尸进程或其他原因,我确实遇到了一些过时的文件描述符问题。
Unfortunately, there isn't such a solution. I managed to do this using a threaded timer that would launch along with the process that would kill it after the timeout but I did run into some stale file descriptor issues because of zombie processes or some such.
不,没有时间限制。我想,您正在寻找的是在一段时间后终止子进程。由于您能够向子进程发出信号,因此您也应该能够杀死它。
向子进程发送信号的通用方法:
您可以使用此机制在超时期限后终止。
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:
You could use this mechanism to terminate after a time out period.
是的, https://pypi.python.org/pypi/python-subprocess2 将扩展Popen 模块有两个附加功能,
这将等待一定的秒数以完成该过程,否则
也返回 None,
这将等待某个时间,然后调用 .terminate(),然后调用 .kill(),其中之一或两者的某种组合,请参阅文档以获取完整详细信息:
http://htmlpreview.github.io/?https://github.com/kata198/python-subprocess2/blob/master/doc/subprocess2.html
Yes, https://pypi.python.org/pypi/python-subprocess2 will extend the Popen module with two additional functions,
This will wait up to acertain number of seconds for the process to complete, otherwise return None
also,
This will wait up to a point, and then call .terminate(), then .kill(), one orthe other or some combination of both, see docs for full details:
http://htmlpreview.github.io/?https://github.com/kata198/python-subprocess2/blob/master/doc/subprocess2.html
对于 Linux,您可以使用信号。这是依赖于平台的,因此 Windows 需要另一个解决方案。不过它可能适用于 Mac。
For Linux, you can use a signal. This is platform dependent so another solution is required for Windows. It may work with Mac though.