进程间通信-队列

#进程间通信,队列
from multiprocessing import  Process,Queue
import os,sys
import time

q=Queue()
def get(data):
    time.sleep(2)
    print("thread {} get {}".format(os.getpid(),data))

if __name__=="__main__":
    start=time.time()
    l=[]
    for i in range(10):
        p=q.put(i)
    print(q.qsize(),os.getpid(),q.empty())
    while True:
        if q.empty() == False:
            p=Process(target=get,args=(q.get(),))
            l.append(p)
            p.start()
        else:
            break
    for i in l:
        i.join()

    end=time.time()
    print(end-start)
10 12072 False
thread 7140 get 3
thread 14184 get 1
thread 4468 get 0
thread 15604 get 2
thread 12988 get 4
thread 7700 get 5
thread 17272 get 7
thread 10700 get 6
thread 13112 get 8
thread 15756 get 9
2.7655727863311768

 

posted @ 2020-03-22 00:06  腹肌猿  阅读(143)  评论(0编辑  收藏  举报