Python, Popen and select - waiting for a process to terminate or a timeout(Python,Popen 和 select - 等待进程终止或超时)
问题描述
我使用以下方式运行子进程:
I run a subprocess using:
p = subprocess.Popen("subprocess",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
此子进程可以在 stderr 上出现错误时立即退出,或者继续运行.我想检测其中任何一种情况 - 后者需要等待几秒钟.
This subprocess could either exit immediately with an error on stderr, or keep running. I want to detect either of these conditions - the latter by waiting for several seconds.
我试过了:
SECONDS_TO_WAIT = 10
select.select([],
[p.stdout, p.stderr],
[p.stdout, p.stderr],
SECONDS_TO_WAIT)
但它只是返回:
([],[],[])
在任何一种情况下.我能做什么?
on either condition. What can I do?
推荐答案
您是否尝试过使用 Popen.Poll() 方法.你可以这样做:
Have you tried using the Popen.Poll() method. You could just do this:
p = subprocess.Popen("subprocess",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
time.sleep(SECONDS_TO_WAIT)
retcode = p.poll()
if retcode is not None:
# process has terminated
这将导致您始终等待 10 秒,但如果失败案例很少见,这将在所有成功案例中摊销.
This will cause you to always wait 10 seconds, but if the failure case is rare this would be amortized over all the success cases.
怎么样:
t_nought = time.time()
seconds_passed = 0
while(p.poll() is not None and seconds_passed < 10):
seconds_passed = time.time() - t_nought
if seconds_passed >= 10:
#TIMED OUT
这有一个忙着等待的丑陋,但我认为它完成了你想要的.
This has the ugliness of being a busy wait, but I think it accomplishes what you want.
另外再看一下选择调用文档,我想你可能想按如下方式进行更改:
Additionally looking at the select call documentation again I think you may want to change it as follows:
SECONDS_TO_WAIT = 10
select.select([p.stderr],
[],
[p.stdout, p.stderr],
SECONDS_TO_WAIT)
由于您通常希望从 stderr 中读取数据,因此您想知道它何时有可读取的内容(即失败案例).
Since you would typically want to read from stderr, you want to know when it has something available to read (ie the failure case).
我希望这会有所帮助.
这篇关于Python,Popen 和 select - 等待进程终止或超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python,Popen 和 select - 等待进程终止或超时
基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01