简体   繁体   English

如何限制线程数

[英]How to limit the number of Threads

There are 101 things stored in THINGS variable. THINGS变量中存储了101个东西。 The code declares 101 threads and executes them all at once instantly all at the same time. 该代码声明101个线程,并立即一次全部立即执行它们。

I wonder if we can limit the number of the active threads to just 12. 我想知道我们是否可以将活动线程数限制为仅12个。

At first only 12 threads should pick their 12 THINGS to process. 起初,只有12个线程应该选择其12种东西进行处理。 The rest of the threads should be waiting for the first 12 to finish their jobs. 其余线程应等待前12个线程完成工作。 When the first 12 threads are all done then the next 12 threads would pick up the next 12 THINGS to process. 当前12个线程全部完成后,接下来的12个线程将处理接下来的12个事件。 And so one. 等等。

Would it be possible? 这有没有可能?

import Queue
import threading, time

class MyThread(threading.Thread):
    def __init__(self, theQueue=None):
        threading.Thread.__init__(self)        
        self.theQueue=theQueue

    def run(self):
        thing=self.theQueue.get()
        self.process(thing) 
        self.theQueue.task_done()

    def process(self, thing):
        time.sleep(1)
        print 'processing %s'%thing.name

queue=Queue.Queue()
THINGS = ['Thing%02d'%i for i in range(101)]

THREADS=[]
for thing in THINGS:
    thread=MyThread(theQueue=queue)
    thread.name = thing
    THREADS.append(thread)
    thread.start() 

for thread in THREADS:       
    queue.put(thread)

The working solution is posted below. 工作解决方案如下。 The basic idea is that we declare only as many Threads instances as there are available CPUs. 基本思想是,我们仅声明与可用CPU一样多的Threads实例。 Then we proceed by adding the "tasks" (or "things" here) to the Queue. 然后,我们将“任务”(或此处的“事物”)添加到队列中。 As soon as the task is added to the queue it is being immediately picked up by one of the Thread instances we declared in the previous step. 一旦将任务添加到队列中,它将立即由我们在上一步中声明的Thread实例之一进行拾取。

Important: In order for this mechanism to work the MyThread.run() method should be running inside of the while loop. 重要说明:为了使该机制起作用, MyThread.run()方法应在while循环内运行。 Otherwise MyThread instance will be terminated as soon as it completes the very first task. 否则,一旦完成第一个任务,MyThread实例将被终止。 The while loop will exit itself after no tasks in the Queue are left. 在队列中没有任何任务后, while循环将自行退出。 That is the end of story. 到此为止。

import Queue
import threading, time

class MyThread(threading.Thread):
    def __init__(self, theQueue=None):
        threading.Thread.__init__(self)        
        self.theQueue=theQueue

    def run(self):
        while True:
            thing=self.theQueue.get()
            self.process(thing) 
            self.theQueue.task_done()

    def process(self, thing):
        time.sleep(1)
        print 'processing %s'%thing

queue=Queue.Queue()
THINGS = ['Thing%02d'%i for i in range(101)]
AVAILABLE_CPUS=3

for OneOf in range(AVAILABLE_CPUS):
    thread=MyThread(theQueue=queue)
    thread.start() # thread started. But since there are no tasks in Queue yet it is just waiting.

for thing in THINGS:       
    queue.put(thing) # as soon as task in added here one of available Threads picks it up

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM