我有一个 python 脚本,它实现了两个级别的多处理:
from multiprocessing import Process, Queue, Lock
if __name__ == '__main__':
pl1s = []
for ip1 in range(10):
pl1 = Process(target=process_level1, args=(...))
pl1.start()
pl1s.append(pl1)
# do somehting for awhile, e.g. for 24 hours
# time to terminate
# ISSUE: this terminates process_level1 processes
# but not process_level2 ones
for pl1 in pl1s:
pl1.terminate()
def process_level1(...):
# subscriibe to external queue
#
with queue.open(name_of_external_queue, 'r') as subq:
qInternal = Queue()
pl2s = []
for ip2 in range(3):
pl2 = Process(target=process_level2, args=(qInternal))
pl2.start()
pl2s.append(pl2)
# grab messages from external queue and push them to
# process_level2 processes to process
#
while True:
message = subq.read()
qInternal.put(m)
def process_level2(qInternal):
while True:
message = qInternal.get()
# do something with date form message
因此,我主要启动了一堆从属子进程,process_level1
每个子进程都启动了一堆自己的子进程process_level2
。Main
应该运行一段预定义的时间(例如 24 小时),然后终止一切。问题是上面的代码终止了第一层子流程,但没有终止第二层。
我应该怎么做才能同时终止两个层?
(也许是一个重要的)警告:我想一种方法是建立一个内部队列来进行通信main
,process_level1
然后向每个process_level1
子进程发送一个信号以终止它们各自的子进程。问题是process_level1
运行一个无限循环读取来自外部队列的消息。所以我不确定在哪里以及如何检查来自 main 的终止信号。