def productor (Myid,dataqueue,num_thread_per_productor): For i in range (Num_thread_per _productor):d ataqueue.put (' producer id:%s ==> %s '% (myid,i)) def consumer (Myid,dataqueue, Stdoutlock): While true:try:data=dataqueue.get (Block=false) except queue. empty:passelse:with stdoutlock:print (' Consumer%s get %s '% (myid,data)) if __name__== ' __main __ ': Import queue,threadingnumproductors=5num_thread_per_productor=5numconsumers=3dataque=queue. Queue () safeprint=threading. Lock () Waitfor=[]for i in range (numproductors):p roductor_thread=threading. Thread (target=productor,args= (I,dataque,num_thread_per_productor)) Productor_thread.start () Waitfor.append ( Productor_thread) For i in range (numconsumers): consumer_thread=threading. Thread (target=consumer,args= (i,dataque,safeprint)) Consumer_thread.daemon=true consumer_ Thread.Start () For wait_thread in waitfor:wait_thread.join () print (" ----------end-----------")
This article is from the "a" blog, please make sure to keep this source http://lzs66.blog.51cto.com/9607068/1854735
Python applet----Simple use of the queue module