进程间通信 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()