CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day13/multiprocess2.py
Views: 729
1
"""
2
实现进程间的通信
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-20
7
"""
8
import multiprocessing
9
import os
10
11
12
def sub_task(queue):
13
print('子进程进程号:', os.getpid())
14
counter = 0
15
while counter < 1000:
16
queue.put('Pong')
17
counter += 1
18
19
20
if __name__ == '__main__':
21
print('当前进程号:', os.getpid())
22
queue = multiprocessing.Queue()
23
p = multiprocessing.Process(target=sub_task, args=(queue,))
24
p.start()
25
counter = 0
26
while counter < 1000:
27
queue.put('Ping')
28
counter += 1
29
p.join()
30
print('子任务已经完成.')
31
for _ in range(2000):
32
print(queue.get(), end='')
33
34