| 1234567891011121314151617181920212223242526272829303132 |
- # coding:utf-8
- from multiprocessing import Process, Queue
- import os
- import time
- import 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__":
- q = Queue()
- pw = Process(target=write, args=(q,))
- pr = Process(target=read, args=(q,))
- pw.start()
- pr.start()
- pw.join() # 等待pw结束
- pr.terminate() # pr进程里是死循环,无法等待其结束,只能强行终止
|