python基础 -- 进程通信

1. 作用

进程共享资源

2. 操作

# Queue 进程通信
from multiprocessing import Process, Queue
import os, time, random

# 写进队列
def write(q):
    print('Process to write: {}.'.format(os.getpid()))
    for value in ['A', 'B', 'C']:
        print('Put {} to queue...'.format(value))
        q.put(value)
        time.sleep(random.random())
    
# 读取队列
def read(q):
    print('Process to read: {}.'.format(os.getpid()))
    while True:
        value = q.get(True)
        print('Get {} from queue.'.format(value))

if __name__ == '__main__':
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    pw.start()
    pr.start()
    pw.join()
    pr.terminate()

你可能感兴趣的:(python基础 -- 进程通信)