为了并行处理某些数据,我想创建2个不同的工作程序。它们各自使用不同的数据源。因此,我确实使用一个工人池来单独称呼他们。 (请参见代码)
我想分别对每个队列使用队列来获取输入数据。第一次运行很正常。但是,如果我为该工作人员添加更多工作,则该工作人员不会处理这些工作。您还可以看到越来越多的工作清单。
我在这里做错了什么?所需的输出将是我放入队列中的每个作业的打印件。
您可以检查下面用Python 3.7编写的代码,该代码应会重现该错误。
奖金问题:如果我在 main 部分中删除了“ time.sleep(0.1)”,并在PowerShell中运行该脚本,则该脚本将在打印第二个工作程序的结果之前完成。如何在此处使用.join以便等待进程运行?
# Creates workers
def worker1(q):
while True:
item = q.get(True)
print('I am Worker 1: I started working, len of the queue is : ' + str(q.qsize()))
print(item)
# Simulating a long calculation
time.sleep(1)
def worker2(q):
while True:
item = q.get(True)
print('I am Worker 2: I started working, len of the queue is : ' + str(q.qsize()))
print(item)
# Simulating a long calculation
time.sleep(1)
if __name__ == '__main__':
# Configs
n_workers = 2
# Define Queues
q1 = multiprocessing.Manager().Queue()
q2 = multiprocessing.Manager().Queue()
the_queue = [q1, q2]
the_pool = []
workers = [worker1, worker2]
# Creating one worker for each Queue
for i in range(n_workers):
the_pool.append(multiprocessing.Pool(1, workers[i],(the_queue[i],)))
# First Run
for i in range(n_workers):
the_queue[i].put("hello " + str(i))
print('I just put in a value, len of the queue is : ' + str(the_queue[i].qsize()))
# Give some time before adding new tasks
time.sleep(0.1)
# Next Runs
for ii in range(10):
for i in range(n_workers):
the_queue[i].put("hello " + str(i))
print('I just put in a value, len of the queue is : ' + str(the_queue[i].qsize()))