1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import  Queue
 4 import  threading
 5 class ThreadPool(object):
 6     def __init__(self,max_num=20):
 7         self.queue=Queue.Queue(max_num)
 8         for i in xrange(max_num):
 9             self.queue.put(threading.Thread)
10     def get_thread(self):
11         return self.queue.get()
12     def add_thread(self):
13         return  self.queue.put(threading.Thread)
14 pool = ThreadPool(10)
15 def func(arg,p):
16     print arg
17     import time
18     time.sleep(2)
19     p.add_thread()
20 for i in xrange(30):
21     thread = pool.get_thread()
22     t = thread(target=func,args=(i,pool))
23     t.start()

 

相关文章:

  • 2021-10-23
  • 2022-01-11
  • 2021-11-25
  • 2022-12-23
  • 2021-12-06
  • 2021-07-29
猜你喜欢
  • 2021-09-17
  • 2021-04-11
  • 2021-08-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
相关资源
相似解决方案