#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