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()

 

posted @ 2017-01-02 00:05  程序员同行者  阅读(1176)  评论(0编辑  收藏  举报