进程间通信 Queue
import multiprocessing
def download_from_web(q):
# 模拟从网上下载数据
data = [11, 22, 33, 44]
for i in data:
q.put(i)
print("---数据下载已写入到队列---")
def analysis_data(q):
waitting_analysis_data = list()
while not q.empty():
waitting_analysis_data.append(q.get())
print(str(waitting_analysis_data) + " <<< 数据拼接完毕")
def main():
# 1. 创建一个队列
q = multiprocessing.Queue(4)
# 2. 创建多个进程,将队列的引用当作实参进行传递到里面
p1 = multiprocessing.Process(target=download_from_web, args=(q,))
p2 = multiprocessing.Process(target=analysis_data, args=(q,))
p1.start()
p2.start()
if __name__ == '__main__':
main()
进程池
from multiprocessing import Pool
import os, time, random
def worker(msg):
t_start = time.time()
print("%s 开始执行,进程号为%d" % (msg, os.getpid()))
time.sleep(random.random() * 2)
t_stop = time.time()
print(msg, "执行完毕, 耗时%.2f" % (t_stop - t_start))
def main():
po = Pool(3)
for i in range(0,10):
po.apply_async(worker, (i,))
print("---start---")
po.close()
po.join()
print("---sop---")
if __name__ == '__main__':
main()