Get a signal once a subprocess ends

狂风中的少年 提交于 2020-01-11 13:08:10

问题


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())

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.


回答1:


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?

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.



来源:https://stackoverflow.com/questions/31841096/get-a-signal-once-a-subprocess-ends

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!