python的多线程数量控制

python多线程如果不进行并发数量控制,在启动线程数量多到一定程度后,会造成线程无法启动的错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
import threading
import Queue
import time


# 重写 python 的多线程类,使之与queue队列相结合
class Thread(threading.Thread):
def __init__(self, queue, **kwargs):
threading.Thread.__init__(self, **kwargs)
self.queue = queue

def run(self):
super(Thread, self).run()
self.queue.get()
self.queue.task_done()


def test(x):
print(x)
time.sleep(x)
print(x)


def main():
q = Queue.Queue(20)
for i in range(100):
q.put(i)
Thread(queue=q, target=test, args=(i,)).start()


if __name__ == '__main__':
main()