gpt4 book ai didi

python - 在 Python 中,我如何知道一个进程何时完成?

转载 作者:IT老高 更新时间:2023-10-28 20:22:51 26 4
gpt4 key购买 nike

我从 Python GUI (PyGTK) 中启动一个进程(使用多处理)。该过程需要很长时间(约 20 分钟)才能完成。该过程完成后,我想对其进行清理(提取结果并加入该过程)。我如何知道该过程何时完成?

我的同事建议在父进程中使用一个繁忙的循环来检查子进程是否已完成。肯定有更好的方法。

在 Unix 中,当一个进程被 fork 时,a signal handler is called from within the parent process when the child process has finished .但我在 Python 中看不到类似的东西。我错过了什么吗?

如何从父进程中观察到子进程的结束? (当然,我不想调用 Process.join(),因为它会卡住 GUI 界面。)

这个问题不限于多处理:我对多线程也有完全相同的问题。

最佳答案

我认为作为使 python 多平台的一部分,像 SIGCHLD 这样的简单事情必须自己完成。同意,当您只想知道 child 何时完成时,这需要做更多的工作,但实际上并没有那么痛苦。考虑以下使用一个子进程来完成工作、两个 multiprocessing.Event 实例和一个检查子进程是否完成的线程:

import threading
from multiprocessing import Process, Event
from time import sleep

def childsPlay(event):
print "Child started"
for i in range(3):
print "Child is playing..."
sleep(1)
print "Child done"
event.set()

def checkChild(event, killEvent):
event.wait()
print "Child checked, and is done playing"
if raw_input("Do again? y/n:") == "y":
event.clear()
t = threading.Thread(target=checkChild, args=(event, killEvent))
t.start()
p = Process(target=childsPlay, args=(event,))
p.start()
else:
cleanChild()
killEvent.set()

def cleanChild():
print "Cleaning up the child..."

if __name__ == '__main__':
event = Event()
killEvent = Event()

# process to do work
p = Process(target=childsPlay, args=(event,))
p.start()

# thread to check on child process
t = threading.Thread(target=checkChild, args=(event, killEvent))
t.start()

try:
while not killEvent.is_set():
print "GUI running..."
sleep(1)
except KeyboardInterrupt:
print "Quitting..."
exit(0)
finally:
print "Main done"

编辑

加入所有创建的进程和线程是一种很好的做法,因为它有助于指示何时创建僵尸(永不完成)进程/线程。我修改了上面的代码,创建了一个继承自 threading.Thread 的 ChildChecker 类。它的唯一目的是在一个单独的进程中启动一个作业,等待该进程完成,然后在一切完成时通知 GUI。加入 ChildChecker 也将加入它正在“检查”的过程。现在,如果进程在 5 秒后没有加入,线程将强制终止进程。输入“y”会创建一个运行“endlessChildsPlay”的子进程,该子进程必须演示强制终止。

import threading
from multiprocessing import Process, Event
from time import sleep

def childsPlay(event):
print "Child started"
for i in range(3):
print "Child is playing..."
sleep(1)
print "Child done"
event.set()

def endlessChildsPlay(event):
print "Endless child started"
while True:
print "Endless child is playing..."
sleep(1)
event.set()
print "Endless child done"

class ChildChecker(threading.Thread):
def __init__(self, killEvent):
super(ChildChecker, self).__init__()
self.killEvent = killEvent
self.event = Event()
self.process = Process(target=childsPlay, args=(self.event,))

def run(self):
self.process.start()

while not self.killEvent.is_set():
self.event.wait()
print "Child checked, and is done playing"
if raw_input("Do again? y/n:") == "y":
self.event.clear()
self.process = Process(target=endlessChildsPlay, args=(self.event,))
self.process.start()
else:
self.cleanChild()
self.killEvent.set()

def join(self):
print "Joining child process"
# Timeout on 5 seconds
self.process.join(5)

if self.process.is_alive():
print "Child did not join! Killing.."
self.process.terminate()
print "Joining ChildChecker thread"
super(ChildChecker, self).join()


def cleanChild(self):
print "Cleaning up the child..."

if __name__ == '__main__':
killEvent = Event()
# thread to check on child process
t = ChildChecker(killEvent)
t.start()

try:
while not killEvent.is_set():
print "GUI running..."
sleep(1)
except KeyboardInterrupt:
print "Quitting..."
exit(0)
finally:
t.join()
print "Main done"

关于python - 在 Python 中,我如何知道一个进程何时完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4995419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com