我在python代码中有一个线程列表,它很安静
threadlist = [thread1, thread2, thread3..........threadn]
其中n大于200,我需要使用python队列一次处理10个线程,我该怎么做。建议深表感谢。
答案 0 :(得分:1)
这似乎是使用threading.Semaphore
课程的好地方。它被明确设计为仅允许有限数量的线程一次访问资源。在主线程中创建一个Semaphore(10),然后让每个子线程在执行开始时调用acquire()
,最后调用release
。那么一次只会运行十个。
这是一个将信号量处理抽象到线程子类的示例;但是你可以在目标函数中轻松地完成它,如果你不介意少一点封装。
from threading import Thread, Semaphore
import time
class PatientThread(Thread):
MAXIMUM_SIMULTANEOUS_THREADS = 10
_semaphore = Semaphore(MAXIMUM_SIMULTANEOUS_THREADS)
def run(self):
PatientThread._semaphore.acquire()
super().run()
PatientThread._semaphore.release()
def exampleTargetFunc(x):
print(f"Thread #{x} is starting.")
time.sleep(1)
print(f"Thread #{x} is completing.")
threads = []
for i in range(200):
threads.append(PatientThread(target=exampleTargetFunc, args=(i,)))
for t in threads:
t.start()
for t in threads:
t.join()
结果:
Thread #0 is starting.
Thread #1 is starting.
Thread #2 is starting.
Thread #3 is starting.
Thread #4 is starting.
Thread #5 is starting.
Thread #6 is starting.
Thread #7 is starting.
Thread #8 is starting.
Thread #9 is starting.
<a one second pause occurs here...>
Thread #2 is completing.
Thread #0 is completing.
Thread #1 is completing.
Thread #10 is starting.
Thread #11 is starting.
Thread #12 is starting.
Thread #4 is completing.
Thread #5 is completing.
Thread #3 is completing.
Thread #7 is completing.
Thread #13 is starting.
Thread #6 is completing.
Thread #16 is starting.
Thread #14 is starting.
Thread #15 is starting.
Thread #17 is starting.
Thread #9 is completing.
Thread #8 is completing.
Thread #18 is starting.
Thread #19 is starting.
<... And so on for the next 20 seconds>
这表明线程10,11和12在0,1和2完成之前没有开始。同样对于螺纹3-9和螺纹13-19。