from multiprocessing import Process,Queue import time import random ''' Two ways to transfer data between processes Project: Production buns and bun Queues and queue threads of the process is not the same Thread queue queue.Queue () --------- process queue multiprocessing.Queue () '' ' DEF AOO (que): the while True: # began producing steamed buns and placed into the queue RR = the random.randint (1,100 ) que.put (rr) Print ( " now produced a number buns% s " % rr) time.sleep(1) DEF Boo (que): the while True: # begin again to take the buns buns to eat queue QG = que.get () Print ( " Now eat buns number% s " % QG) if __name__ == "__main__": que = Queue () # create a process queue Pros = [] for i in the Range (3): # Create three buns production process Pro1 = Process (target = AOO, args = (que,)) pros.append (Pro1) # the function passed to process que shared queue pro1.start () pro2 = Process(target=boo,args=(que,)) pros.append(pro2) pro2.start () # create a process bun for PR in Pros: # all processes executed after their execution pr.join () Print ( " ------- I am the Lord Process Process " )
from multiprocessing import Pipe,Process def aoo(conn): conn.send("ddd") print(conn.recv()) if __name__ == "__main__": parent_conn, child_conn = Pipe () # create a pipe to facilitate information exchange Pro = Process (target = AOO, args = (child_conn,)) # creation process, and the child process is transmitted to the function Conn pro.start () Print (parent_conn.recv ()) parent_conn.send("jjjjj") pro.join()