原因:显示了如何使用队列将工作交付给一组工作进程并收集结果的示例
# coding=utf-8
import time
import logging
import multiprocessing
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [*] %(message)s"
)
import time
import random
from multiprocessing import current_process, freeze_support
def worker (task_queue, output):
for func, args in iter(task_queue.get, 'STOP'):
result = calculate(func, args)
output.put(result)
def calculate (func, args):
result = func(*args)
return f'{current_process().name} 操作 {func.__name__}{args} = {result}'
# 乘法
def mul (a, b):
time.sleep(0.5 * random.random())
return a * b
# 加法
def add (a, b):
time.sleep(0.5 * random.random())
return a + b
def main (ctx):
NUMBER_OF_PROCESSES = 4
TASKS1 = [(mul, (i, 7)) for i in range(20)] # 20个乘法运算
TASKS2 = [(add, (i, 8)) for i in range(10)] # 10个加法运算
task_queue = ctx.Queue() # 乘法运算队列(不是固定的乘法运算操作队列,下面代码也会加入加法运算操作)
result_queue = ctx.Queue()
# 乘法运算加入队列-----------------------------------------------------------------------------
for task in TASKS1:
task_queue.put(task)
for i in range(NUMBER_OF_PROCESSES):
ctx.Process(target=worker, args=(task_queue, result_queue)).start()
print("获取乘法运算的结果:----------------------[*]")
# 获取乘法运算的结果---------------------------------------------------------------------------
for i in range(len(TASKS1)):
print('\t', result_queue.get())
# 加法运算加入队列-----------------------------------------------------------------------------
for task in TASKS2:
task_queue.put(task)
print("获取加法运算的结果:----------------------[*]")
# 获取加法运算的结果---------------------------------------------------------------------------
for i in range(len(TASKS2)):
print('\t', result_queue.get())
# 让四个子进程停止遍历队列---------------------------------------------------------------------
for i in range(NUMBER_OF_PROCESSES):
task_queue.put('STOP')
if __name__ == '__main__':
freeze_support()
# windows 启动方式
multiprocessing.set_start_method('spawn')
# 获取上下文
ctx = multiprocessing.get_context('spawn')
main(ctx)
输出:
2019-10-04 23:50:01,483 [*] 获取乘法运算的结果:----------------------[*]
2019-10-04 23:50:01,640 [*] SpawnProcess-1 操作 mul(2, 7) = 14
2019-10-04 23:50:01,667 [*] SpawnProcess-3 操作 mul(1, 7) = 7
2019-10-04 23:50:01,781 [*] SpawnProcess-1 操作 mul(4, 7) = 28
2019-10-04 23:50:01,927 [*] SpawnProcess-3 操作 mul(5, 7) = 35
2019-10-04 23:50:01,948 [*] SpawnProcess-4 操作 mul(3, 7) = 21
2019-10-04 23:50:02,011 [*] SpawnProcess-2 操作 mul(0, 7) = 0
2019-10-04 23:50:02,101 [*] SpawnProcess-1 操作 mul(6, 7) = 42
2019-10-04 23:50:02,227 [*] SpawnProcess-4 操作 mul(8, 7) = 56
2019-10-04 23:50:02,304 [*] SpawnProcess-2 操作 mul(9, 7) = 63
2019-10-04 23:50:02,379 [*] SpawnProcess-3 操作 mul(7, 7) = 49
2019-10-04 23:50:02,508 [*] SpawnProcess-1 操作 mul(10, 7) = 70
2019-10-04 23:50:02,595 [*] SpawnProcess-4 操作 mul(11, 7) = 77
2019-10-04 23:50:02,624 [*] SpawnProcess-2 操作 mul(12, 7) = 84
2019-10-04 23:50:02,792 [*] SpawnProcess-3 操作 mul(13, 7) = 91
2019-10-04 23:50:02,827 [*] SpawnProcess-4 操作 mul(15, 7) = 105
2019-10-04 23:50:02,848 [*] SpawnProcess-2 操作 mul(16, 7) = 112
2019-10-04 23:50:02,937 [*] SpawnProcess-1 操作 mul(14, 7) = 98
2019-10-04 23:50:03,057 [*] SpawnProcess-4 操作 mul(18, 7) = 126
2019-10-04 23:50:03,150 [*] SpawnProcess-2 操作 mul(19, 7) = 133
2019-10-04 23:50:03,275 [*] SpawnProcess-3 操作 mul(17, 7) = 119
2019-10-04 23:50:03,276 [*] 获取加法运算的结果:----------------------[*]
2019-10-04 23:50:03,407 [*] SpawnProcess-4 操作 add(1, 8) = 9
2019-10-04 23:50:03,457 [*] SpawnProcess-4 操作 add(4, 8) = 12
2019-10-04 23:50:03,568 [*] SpawnProcess-1 操作 add(0, 8) = 8
2019-10-04 23:50:03,608 [*] SpawnProcess-4 操作 add(5, 8) = 13
2019-10-04 23:50:03,670 [*] SpawnProcess-1 操作 add(6, 8) = 14
2019-10-04 23:50:03,720 [*] SpawnProcess-2 操作 add(2, 8) = 10
2019-10-04 23:50:03,738 [*] SpawnProcess-3 操作 add(3, 8) = 11
2019-10-04 23:50:03,906 [*] SpawnProcess-2 操作 add(9, 8) = 17
2019-10-04 23:50:03,937 [*] SpawnProcess-4 操作 add(7, 8) = 15
2019-10-04 23:50:04,018 [*] SpawnProcess-1 操作 add(8, 8) = 16