Python Multithreading limit concurrency number example

Source: Internet
Author: User

#Coding:utf-8#!/usr/bin/env pythonImportQueueImportThreadingImportTimeprolock=Threading. Lock ()#define the number of simultaneous queuesQueue = Queue.queue (maxsize=10)#define task initial value and maximum valueTaskidx =0maxidx= 100#Build a task listdeftaskList (): Task= []     forIinchRange (100): Task.append ("Task"+str (i))returnTask#put the task into the queueclassProducer (Threading. Thread):def __init__(self, Name, queue): self.__name=name self.__queue=Queue Super (Producer, self).__init__()    defRun (self): whileTrue:GlobalTaskidx, Prolock, Maxidx time.sleep (4) Prolock.acquire ()Print 'Producer Name:%s'% (self.__name)            ifMaxidx = =taskidx:prolock.release () BreakIPs=taskList () IP=Ips[taskidx] self.__queue. Put (IP) taskidx= Taskidx + 1prolock.release ()#Thread processing TasksclassConsumer (Threading. Thread):def __init__(self, Name, queue): self.__name=name self.__queue=Queue Super (Consumer, self).__init__()    defRun (self): whileTrue:ip= self.__queue. Get ()Print 'Consumer Name:%s'% (self.__name) consumer_process (IP) self.__queue. Task_done ()defconsumer_process (IP): Time.sleep (1)    PrintIPdefStartproducer (thread_num): T_produce= []     forIinchRange (thread_num): P= Producer ("producer"+Str (i), queue) P.setdaemon (True) P.start () T_produce.append (p)returnT_producedefStartconsumer (thread_num): T_consumer= []     forIinchRange (Thread_num): C= Consumer ("Consumer"+Str (i), queue) C.setdaemon (True) C.start () T_consumer.append (c)returnT_consumerdefMain (): T_produce= Startproducer (3) T_consumer= Startconsumer (5)    #Make sure all the tasks are generated     forPinchT_produce:p.join ()#wait for all tasks to finish processingQueue.join ()if __name__=='__main__': Main ()Print '------End-------'

The general build task is faster, and you can use a single thread to build the task, as in the following example:

#Coding:utf-8#!/usr/bin/env pythonImportQueueImportThreadingImport Time#define how many tasks are processed concurrentlyQueue = Queue.queue (maxsize=3)#Build a task listdeftaskList (): Task= []     forIinchRange (100): Task.append ("Task"+str (i))returnTask#put the task into the queueclassProducer (Threading. Thread):def __init__(self, Name, queue): self.__name=name self.__queue=Queue Super (Producer, self).__init__()    defRun (self): forIpinchtaskList (): Self.__queue. Put (IP)#Thread processing TasksclassConsumer (Threading. Thread):def __init__(self, Name, queue): self.__name=name self.__queue=Queue Super (Consumer, self).__init__()    defRun (self): whileTrue:ip= self.__queue. Get ()Print 'Consumer Name:%s'% (self.__name) consumer_process (IP) self.__queue. Task_done ()defconsumer_process (IP): Time.sleep (1)    PrintIPdefStartconsumer (thread_num): T_consumer= []     forIinchRange (Thread_num): C=Consumer (i, queue) C.setdaemon (True) C.start () T_consumer.append (c)returnT_consumerdefMain (): P= Producer ("Producer Task0", queue) P.setdaemon (True) P.start () Startconsumer (9)    #Make sure all the tasks are generatedP.join ()#wait for all tasks to finish processingQueue.join ()if __name__=='__main__': Main ()Print '------End-------'

Python Multithreading limit concurrency number example

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.