首页 > 编程语言 > 详细

简版线程池

时间:2017-02-25 21:20:25      阅读:207      评论:0      收藏:0      [点我收藏+]
 1 import queue
 2 import threading
 3 
 4 
 5 class ThreadPool(object):
 6 
 7     def __init__(self, max_num=20):
 8         self.queue = queue.Queue(max_num)
 9         for i in range(max_num):
10             self.queue.put(threading.Thread)
11 
12     def get_thread(self):
13         return self.queue.get()
14 
15     def add_thread(self):
16         self.queue.put(threading.Thread)
17 
18 pool = ThreadPool(10)
19 def func(arg, p):
20     print(arg)
21     import time
22     time.sleep(2)
23     p.add_thread()
24 
25 
26 for i in range(30):
27     thread = pool.get_thread()
28     t = thread(target=func, args=(i, pool))
29     t.start()

 

简版线程池

原文:http://www.cnblogs.com/shiluoliming/p/6442729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!