#-*-coding:utf-8-*-"""multi-threaded simultaneous Read Queue summary: 1. Blocks if self._jobq.qsize () > 0 into logic, at which time the data is taken out by other threads, and 2 is blocked in data = Self._jobq.get (). Need to learn to lock the use Logic: * Main Line Chengti go to queue write all data * sub-thread reads queue data without exiting thread"""ImportQueueImportThreadingImport TimeImportRANDOMQ= Queue.queue (0)#Infinite-size queuesNum_workers = 3#Number of ThreadsclassMyThread (Threading. Thread):"""reading data from a queue print""" def __init__(self,queue,worktype):""":p Aram Queue: Queues:p Aram worktype: Additional parameters"""Threading. Thread.__init__(self) SELF._JOBQ=Queue Self._work_type=WorktypedefRun (self): whileTrue:ifSelf._jobq.qsize () >0:time.sleep (Random.random ()* 3) #Get Queue Datadata =Self._jobq.get ()Print "doing", data,"Worktype", Self._work_typeElse: Print "%d,end"%Self._work_type Breakif __name__=='__main__': Print "begin ...." #Write Data to the queue forIinchRange (Num_workers * 2): Q.put (i)Print "Job Qsize:", Q.qsize ()#Start Thread forXinchRange (num_workers): MyThread (q,x). Start ()Print "End"" "out:begin....job qsize:6enddoing 0 worktype 1doing 1 worktype 0doing 2 worktype 2doing 3 Worktype 1doing 4 W Orktype 2doing 5 worktype 00,end blocked ..." "
Python Induction (13) _ Queuing queue used in multi-threading