python - Python 中的事件驱动系统调用

标签 python progress-bar subprocess event-driven

我正在尝试使用系统调用或子流程来实现事件驱动流程。基本上我想启动一个非阻塞系统命令,并在完成该系统调用后,我想要调用一个函数。 这样我就可以启动 GUI 进度条,启动系统命令并让进度条继续,当系统调用完成时,让进度条停止。

我绝对不想做的是生成一个进程,获取其进程 ID 并在 while 循环中继续检查该进程的完成情况。

下面只是我想象的一个示例(所有这些都在一个类中)

def launchTool(self):

    self.progressbar.config(mode = 'indeterminate')
    self.progressbar.start(20)
    self.launchButton.config(state = 'disabled')
    self.configCombobox.config(state = 'disabled')

    ##  here the "onCompletion" is a pointer to a function
    call("/usr/bin/make psf2_dcf", shell=True, onCompletion = self.toolCompleted)


def onCompletion(self):

    print('DONE running Tool')

    self.progressbar.stop()
    self.launchButton.config(state = 'normal')
    self.configCombobox.config(state = 'normal')

最佳答案

为了避免轮询子进程的状态,您可以在 Unix 上使用 SIGCHLD 信号。要将其与 tkinter 的事件循环结合起来,您可以使用 the self-pipe trick 。它还有解决方法the possible tkinter + signal issue无需定期唤醒事件循环。

#!/usr/bin/env python3
import logging
import os
import signal
import subprocess
import tkinter

info = logging.getLogger(__name__).info

def on_signal(pipe, mask, count=[0]):
    try:
        signals = os.read(pipe, 512)
    except BlockingIOError:
        return # signals have been already dealt with

    # from asyncio/unix_events.py
    #+start
    # Because of signal coalescing, we must keep calling waitpid() as
    # long as we're able to reap a child.
    while True:
        try:
            pid, status = os.waitpid(-1, os.WNOHANG)
        except ChildProcessError:
            info('No more child processes exist.')
            return
        else:
            if pid == 0:
                info('A child process is still alive. signals=%r%s',
                     signals, ' SIGCHLD'*(any(signum == signal.SIGCHLD
                                              for signum in signals)))
                return
            #+end
            # you could call your callback here
            info('{pid} child exited with status {status}'.format(**vars()))
            count[0] += 1
            if count[0] == 2:
                root.destroy() # exit GUI


logging.basicConfig(format="%(asctime)-15s %(message)s", datefmt='%F %T',
                    level=logging.INFO)
root = tkinter.Tk()
root.withdraw() # hide GUI

r, w = os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC) 
signal.set_wakeup_fd(w) 
root.createfilehandler(r, tkinter.READABLE, on_signal)
signal.signal(signal.SIGCHLD, lambda signum, frame: None) # enable SIGCHLD
signal.siginterrupt(signal.SIGCHLD, False) # restart interrupted syscalls automatically
info('run children')
p = subprocess.Popen('sleep 4', shell=True)
subprocess.Popen('sleep 1', shell=True)
root.after(2000, p.send_signal, signal.SIGSTOP) # show that SIGCHLD may be delivered
root.after(3000, p.send_signal, signal.SIGCONT) # while the child is still alive
root.after(5000, lambda: p.poll() is None and p.kill()) # kill it
root.mainloop()
info('done')

输出

2015-05-20 23:39:50 run children
2015-05-20 23:39:51 16991 child exited with status 0
2015-05-20 23:39:51 A child process is still alive. signals=b'\x11' SIGCHLD
2015-05-20 23:39:52 A child process is still alive. signals=b'\x11' SIGCHLD
2015-05-20 23:39:53 A child process is still alive. signals=b'\x11' SIGCHLD
2015-05-20 23:39:54 16989 child exited with status 0
2015-05-20 23:39:54 No more child processes exist.
2015-05-20 23:39:54 done

关于python - Python 中的事件驱动系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087506/

相关文章:

c# - ProgressBar 的默认前景色

java用于多个文件下载的进度条

python-3.x - 往返 2 个 Python 子进程的循环管道

python - 子进程返回代码不同于 "echo $?"

python - 主成分分析不起作用

python - 使用 Flask Classy 设置 Flask

python - 将字符串转换为 ASCII 值 python

python - 类型提示返回 NameError : name 'datetime' not defined

android - 如何根据进度条的填充程度更改进度条的颜色?

Python:结束一个无限迭代的子进程