Python 线程池
import Queue, threading, sys
from threading import Thread
import time,urllib
# working thread
class Worker(Thread):
worker_count = 0
def __init__( self, workQueue, resultQueue, timeout = 0, **kwds):
Thread.__init__( self, **kwds )
self.id = Worker.worker_count
Worker.worker_count += 1
self.setDaemon( True )
self.workQueue = workQueue
self.resultQueue = resultQueue
self.timeout = timeout
self.start( )
def run( self ):
''' the get-some-work, do-some-work main loop of worker threads '''
while True:
try:
callable, args, kwds = self.workQueue.get(timeout=self.timeout)
res = callable(*args, **kwds)
print "worker[%2d]: %s" % (self.id, str(res) )
self.resultQueue.put( res )
except Queue.Empty:
break
except :
print 'worker[%2d]' % self.id, sys.exc_info()[:2]
class WorkerManager:
def __init__( self, num_of_workers=10, timeout = 1):
self.workQueue = Queue.Queue()
self.resultQueue = Queue.Queue()
self.workers = []
self.timeout = timeout
self._recruitThreads( num
相关文档:
一个有趣的网站:
http://www.pythonchallenge.com/
集娱乐与学习于一体,在开动脑筋闯关的过程中,不但扩展了思维,还对Python加深了理解。
一共33关,每闯过一关都可以在提示下查看作者给出的Solution。
第0关(指导关):
出现一幅画面,上面写着2**38,教你如何进入下一关。
&nb ......
近来需要用Python对MySQL数据库进行操作。Python久闻其名,未见其“人”;数据库曾经很高分,早已还给先生,更无MySQL经验。于是一切从零开始,借机好好学习一番。
Python这个脚本语言确实名副其实,用了几天便喜欢上它啦。很简洁,很方便。以缩减作为模块分割符,读起来赏心悦目。python的内建数据类型( ......
为了google的google appengine而学习python
发现他真的不如c语言好学,倒不是他难懂,而是相关文档太少,而且不规范。
也许是我的问题吧,我还是喜欢c语言的头文件,还有msdn的那种帮助。一眼就知道使用方法。
说c语言难学,可能是因为没有仔细看这两个东西吧 ......
2009-11-16
Collin Winter是Python社区一位颇具影响力的开发者,他曾是CPython项目的核心开发者之一、也曾是Unladen Swallow(见文末注释)的核心开发者,参与了很多Python项目的开发。近来传闻Google将在其新项目中限制Python的使用,为此有开发者(以K表示)在Google 论坛中公开询问了Collin Winter,Collin Winte ......