【发布时间】:2021-09-22 17:23:21
【问题描述】:
我正在使用 python 多处理池和队列来并行运行任务。 但我必须在队列中动态插入其他作业并等待它们完成(他们也可以在队列中插入其他作业)。
def add_another(q,blocked_name):
name = q.get()
if 'a' in name and name not in blocked_name:
print('The name contains an a')
# Here I want to add another name in the queue
# Like q.put('Another') even if 'Another' will create a loop
if __name__ == '__main__':
m = Manager()
q = m.Queue()
p = Pool(3)
worker = []
req = ['alice','amanda','bob','mallery']
for d in req:
q.put(d)
blocked_name = ['mallery','steve']
for i in range(len(req)):
worker.append(p.apply_async(add_another, (q,blocked_name,)))
# Here I want to wait ALL the worker, even the one added inside the add_another method
[r.get() for r in worker]
我该怎么做?
感谢您的帮助
【问题讨论】:
-
您的目标是从一个初始子进程中添加一个额外的工作进程吗?
标签: python multithreading queue threadpool