Python中的事件驱动系统调用 [英] Event Driven System Call in Python

查看:270
本文介绍了Python中的事件驱动系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过系统调用或子流程来实现事件驱动的流程.基本上,我想启动一个非阻塞系统命令,并且在该系统调用完成后,我希望一个函数被调用. 这样一来,我可以启动GUI进度栏,启动系统命令并使进度栏继续运行,并在系统调用完成时停止进度栏.

I'm trying to implement an event driven process with system call or subprocess. Basically I want to launch a non-blocking system command and upon completion of that system call, I want a function to be called. This is so that I can start a GUI progress bar, launch a system command and have the progress bar continue, and when the system call finishes, have the progress bar stop.

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

What I want to absolutely NOT DO, is to spawn a process, get its process ID and keep checking for the completion of that process in a while loop.

下面只是一个我如何想象它应该工作的示例(所有这些都在一个类内)

Below is just an example of how I imagine this should work (All of these are inside a class)

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的事件循环结合使用,可以使用自管道技巧.它还解决了可能的tkinter +信号问题,而没有需要定期唤醒事件循环.

To avoid polling subprocess' status, you could use SIGCHLD signal on Unix. To combine it with tkinter's event loop, you could use the self-pipe trick. It also workarounds the possible tkinter + signal issue without the need to wake the event loop periodically.

#!/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中的事件驱动系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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