我正在尝试使用系统调用或子进程实现事件驱动的进程。基本上我想启动一个非阻塞系统命令,并在完成该系统调用后,我想要一个函数被调用。 这样我就可以启动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')
答案 0 :(得分:1)
为避免轮询子进程'状态,可以在Unix上使用for word in movie_reviews.words(categories=['neg']):
print(word) #Put this line here and print ans whatch what is happening
word_fd[word.lower()] += 1
label_word_fd['neg'][word.lower()] += 1
信号。要将它与tkinter的事件循环结合使用,您可以使用the self-pipe trick。它还解决了the possible tkinter + signal issue的问题,而无需定期唤醒事件循环。
SIGCHLD
#!/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')