如何创建多个进程?
进程之间如何实现通信?
使用队列实现进程之间的数据共享!!!
from multiprocessing import Queue,Process import time,random list1 = ["Java","Python","C","Go"] def write(queue): for value in list1: print(f"正在向队列中添加数据{value}") queue.put(value) time.sleep(random.random()) def read(queue): while True: if not queue.empty(): value = queue.get() print(f"从队列中读取的数据为{value}") time.sleep(random.random()) else: break queue = Queue() write_data = Process(target=write,args=(queue,)) read_data = Process(target=read,args=(queue,)) if __name__ == '__main__': write_data.start() write_data.join() read_data.start() read_data.join() print('OK')