子流程结束后获取信号 [英] Get a signal once a subprocess ends

查看:85
本文介绍了子流程结束后获取信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用subprocess.Popen函数来运行命令行.无需使用Popen.wait(),我想在子进程使用完Popen.poll()之后检查它.有关如何执行此操作的任何建议?

I am using the subprocess.Popen function to run a command line. Without having to use Popen.wait(), I want to check the subprocess after it has finished using Popen.poll(). Any suggestions on how to do this?

import subprocess
job = subprocess.Popen('command line', shell = True)
print(job.poll())

就这样,我在子进程开始之前打印了job.poll().我要它等到结束.我不想使用wait,因为直到该过程结束,其余的用户界面才变得不可用.这是在PyQt4中.

As it is, I get job.poll() printed before the subprocess starts. I want it to wait until it ends. I don't want to use wait because the rest of the user interface becomes unusable until the process ends. This is in PyQt4.

推荐答案

As Python - wait on a condition without high cpu usage says, there are only two ways in existence to wait for something: polling or setting up/using a notification system.

如果是用户界面-您是否没有忘记自己始终拥有的一个通知系统-消息队列?

If it's UI - didn't you forget about one notification system you always have - the message queue?

此外,您可以始终(如果是UI,则始终为 )在工作线程中执行任何耗时的任务.在这种情况下,您可以进行同步通话.

Besides, you can always (and, if it's UI, should always) perform any time-consuming tasks in worker threads. If which case, you are just fine with a synchronous call.

这篇关于子流程结束后获取信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆