一、low版
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import threading 4 import queue 5 import time 6 class threadpool: 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 def get_thread(self): 12 return self.queue.get() 13 def add_thread(self): 14 self.queue.put(threading.Thread) 15 16 def fun(pool,a1): 17 time.sleep(1) 18 print(a1) 19 pool.add_thread() 20 p = threadpool() 21 for i in range(50): 22 ret = p.get_thread() 23 t= ret(target= fun,args=(p,i,)) 24 t.start()
原文:http://www.cnblogs.com/Erick-L/p/6502576.html