python 多线程实例
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()
不要让懒惰占据你的大脑,不让要妥协拖跨你的人生。青春就是一张票,能不能赶上时代的快车,你的步伐掌握在你的脚下,good luck