线程小demo
下午就手写了两个demo,整理了一下。
#!/sur/bin/env python # -*- coding:utf-8 -*- __author__ = 'ganzl' import threading def Foo(arg): print 'print thread: %s' %(arg) for i in range(10): t = threading.Thread(target=Foo,args=(i,)) t.start()
#!/sur/bin/env python # -*- coding:utf-8 -*- __author__ = 'ganzl' import threading import Queue class PoolThread: def __init__(self,max_int=20): self.queue = Queue.Queue(max_int) for i in xrange(max_int): self.queue.put(threading.Thread) def use_thread(self): return self.queue.get() def add_thread(self): self.queue.put(threading.Thread) pool = PoolThread(10) print pool def run(i,p): print i import time time.sleep(2) p.add_thread() for i in xrange(30): t = pool.use_thread() a = t(target=run,args=(i,pool),) a.start()
http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
这个写的真好.