Python Induction (13) _ Queuing queue used in multi-threading

Source: Internet
Author: User

#-*-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.