from multiprocessing import Pool, Manager
import time, random, os
# 需要执行的函数
def f(x):
time.sleep(random.randint(1, 3))
return x, os.getpid()
# 回调函数
def p_callback(res):
x, pid = res
print("回调进程ID号:%s, 结果:%s" % (pid, x))
def m_callback(res):
d, pid = res
d.value += 1
print("回调进程ID号:%s, 结果:%s" % (pid, d))
if __name__ == '__main__':
with Pool(4) as p:
print("----------map----------")
# 会阻塞,直到所有的进程都执行完毕
print(p.map(f, [1, 2, 3]))
print("----------imap----------")
# 阻塞懒惰map,需要调用结果next 驱动任务继续执行
it = p.imap(f, range(10))
for x in it:# 这里相当于主动调用了 next
print("\t", x)
print("----------imap_unordered----------")
# 与map一样都是阻塞懒惰模式,但是imap_unordered不保证结果顺序
it = p.imap_unordered(f, range(10))
for x in it:
print("\t", x)
with Pool(4) as p:
for i in range(10):
# 异步,非阻塞方式执行,主进程需要等待子进程完成
p.apply_async(f, (i,), callback=p_callback)
print("-------- apply_async --------")
p.close()
p.join() # 主进程等待子进程结束
# 进程共享对象
with Manager() as m:
v = m.Value('i', 0) # i ctype int 类型
# 支持类型:list,dict,Namespace,Lock, RLock,Semaphore,BoundedSemaphore,
# Condition,Event,Barrier, Queue,Value和Array。例如,
with Pool(4) as p:
for i in range(10):
p.apply_async(f, args=(v,), callback=m_callback)
p.close()
p.join()
结果如下:
D:\software\Anaconda3\pythonw.exe D:/workspace/PycharmProjects/DeltaGrad/trunk/strategy/load/thread_test.py
----------map----------
[(1, 12344), (2, 22720), (3, 21940)]
----------imap----------
(0, 20732)
(1, 12344)
(2, 22720)
(3, 21940)
(4, 22720)
(5, 21940)
(6, 12344)
(7, 22720)
(8, 20732)
(9, 12344)
----------imap_unordered----------
(1, 21940)
(2, 20732)
(4, 21940)
(0, 22720)
(3, 12344)
(8, 21940)
(5, 20732)
(7, 12344)
(6, 22720)
(9, 21940)
-------- apply_async --------
回调进程ID号:17424, 结果:1
回调进程ID号:21744, 结果:2
回调进程ID号:23220, 结果:0
回调进程ID号:17424, 结果:4
回调进程ID号:21744, 结果:5
回调进程ID号:1360, 结果:3
回调进程ID号:21744, 结果:8
回调进程ID号:23220, 结果:6
回调进程ID号:17424, 结果:7
回调进程ID号:1360, 结果:9
回调进程ID号:3164, 结果:Value('i', 1)
回调进程ID号:18120, 结果:Value('i', 2)
回调进程ID号:18116, 结果:Value('i', 3)
回调进程ID号:22108, 结果:Value('i', 4)
回调进程ID号:18120, 结果:Value('i', 5)
回调进程ID号:3164, 结果:Value('i', 6)
回调进程ID号:3164, 结果:Value('i', 7)
回调进程ID号:18116, 结果:Value('i', 8)
回调进程ID号:22108, 结果:Value('i', 9)
回调进程ID号:18120, 结果:Value('i', 10)