from multiprocessing import Process, Queue
import os, time, random
# 写数据进程执行的代码:
def write(q):
print('Process to write: %s' % os.getpid())
for value in ['A', 'B', 'C']:
print('Put %s to queue...' % value)
q.put(value)
time.sleep(random.random())
# 读数据进程执行的代码:
def read(q):
print('Process to read: %s' % os.getpid())
while True:
value = q.get(True)
print('Get %s from queue.' % value)
if __name__=='__main__':
# 父进程创建Queue,并传给各个子进程:
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
# 启动子进程pw,写入:
pw.start()
# 启动子进程pr,读取:
pr.start()
# 等待pw结束:
pw.join()
# pr进程里是死循环,无法等待其结束,只能强行终止:
pr.terminate()# your code goes here
ZnJvbSBtdWx0aXByb2Nlc3NpbmcgaW1wb3J0IFByb2Nlc3MsIFF1ZXVlCmltcG9ydCBvcywgdGltZSwgcmFuZG9tCgojIOWGmeaVsOaNrui/m+eoi+aJp+ihjOeahOS7o+eggToKZGVmIHdyaXRlKHEpOgogICAgcHJpbnQoJ1Byb2Nlc3MgdG8gd3JpdGU6ICVzJyAlIG9zLmdldHBpZCgpKQogICAgZm9yIHZhbHVlIGluIFsnQScsICdCJywgJ0MnXToKICAgICAgICBwcmludCgnUHV0ICVzIHRvIHF1ZXVlLi4uJyAlIHZhbHVlKQogICAgICAgIHEucHV0KHZhbHVlKQogICAgICAgIHRpbWUuc2xlZXAocmFuZG9tLnJhbmRvbSgpKQoKIyDor7vmlbDmja7ov5vnqIvmiafooYznmoTku6PnoIE6CmRlZiByZWFkKHEpOgogICAgcHJpbnQoJ1Byb2Nlc3MgdG8gcmVhZDogJXMnICUgb3MuZ2V0cGlkKCkpCiAgICB3aGlsZSBUcnVlOgogICAgICAgIHZhbHVlID0gcS5nZXQoVHJ1ZSkKICAgICAgICBwcmludCgnR2V0ICVzIGZyb20gcXVldWUuJyAlIHZhbHVlKQoKaWYgX19uYW1lX189PSdfX21haW5fXyc6CiAgICAjIOeItui/m+eoi+WIm+W7ulF1ZXVl77yM5bm25Lyg57uZ5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