importmultiprocessingfrommultiprocessingimportProcess,Event,Queueimporttimedefreader(data):input_queue=data[0]e=data[1]output_queue=data[2]whileTrue:ifnote.is_set():# if there is a signal to start#if not output_queue.empty(): # hangs somewhymsg=input_queue.get()# Read from the queueoutput_queue.put(msg)# copy to output_queue#print("1")if(msg=='DONE'):# signal to stope.set()# signal that there is no more dataprint("done")defreader1(data):input_queue=data[0]e=data[1]output_queue=data[2]whileTrue:ifnote.is_set():# if there is a signal to startmsg=input_queue.get()# Read from the queueoutput_queue.put(msg)# copy to output_queue#print("2")if(msg=='DONE'):# signal to stope.set()# signal that there is no more dataprint("done")defreader2(data):input_queue=data[0]e=data[1]output_queue=data[2]whileTrue:ifnote.is_set():# if there is a signal to startmsg=input_queue.get()# Read from the queueoutput_queue.put(msg)# copy to output_queue#print("3")if(msg=='DONE'):# signal to stope.set()# signal that there is no more dataprint("done")defwriter(count,queue):## Write to the queueforiiinrange(0,count):queue.put(ii)# Write 'count' numbers into the queuequeue.put('DONE')if__name__=='__main__':# I do not use manager, as it makes everything extremely slow#m = multiprocessing.Manager()#input_queue = m.Queue()input_queue=Queue()# reader() reads from queue# writer() writes to queueoutput_queue=Queue()e=Event()e.set()reader_p=Process(target=reader,args=((input_queue,e,output_queue),))reader_p.daemon=Truereader_p.start()# Launch reader() as a separate python processreader_p1=Process(target=reader1,args=((input_queue,e,output_queue),))reader_p1.daemon=Truereader_p1.start()reader_p2=Process(target=reader2,args=((input_queue,e,output_queue),))reader_p2.daemon=Truereader_p2.start()forcountin[10**4,10**5,10**6]:_start=time.time()writer(count,input_queue)# Send a lot of stuff to readerse.clear()# unset event, giving signal to a workere.wait()# waiting for reader to finish# fetch results from output_queue:results=[]whilenotoutput_queue.empty():results+=[output_queue.get()]print(len(results))# check how many results we haveprint("Sending %s numbers to Queue() took %s seconds"%(count,(time.time()-_start)))
python multiprocessing queue_Python:multiprocessing.Queue()中可能的数据丢失
最新推荐文章于 2024-04-07 13:43:10 发布