twisted的持久进程池
txpool的Python项目详细描述
摘要
用于 Twisted。提供运行的能力 python可在持久进程池中异步调用, 只要可调用,它的参数和返回值都是 可摘。
安装
pip install txpool
或
python setup.py install
示例
下面是一些简单的例子,让您了解这个想法:
importglobfromtwisted.internetimportreactorfromtwisted.internet.deferimportinlineCallbacksimporttxpoolpool=txpool.Pool()@inlineCallbacksdefmain():result=yieldpool.apply_async(glob.glob,('*.pdf',))printresultreactor.stop()reactor.callWhenRunning(main)reactor.run()
可调用的可以改为使用点符号指定为字符串 指定可调用的完整路径。
fromtwisted.internetimportreactorfromtwisted.internet.deferimportinlineCallbacksimporttxpoolpool=txpool.Pool()@inlineCallbacksdefmain():# You can provide an optional timeout (in seconds) for the call# (the default is None).try:result=yieldpool.apply_async('glob.glob',('*.pdf',),timeout=5)exceptPoolTimeoutase:result=eprintresultreactor.stop()reactor.callWhenRunning(main)reactor.run()
可以显式地调整txpool.pool类的大小,并要求记录它的 操作和/或给定自定义名称。
importloggingfromtwisted.internetimportreactorfromtwisted.internet.deferimportinlineCallbacks,gatherResultsimporttxpoollogger=logging.getLogger('example')logger.addHandler(logging.StreamHandler())logger.setLevel(logging.DEBUG)pool=txpool.Pool(size=5,log=logger,name='twisting-by-the-pool')@inlineCallbacksdefmain():calls=('math.factorial',)*5args=[(n,)forninrange(150780,150785)]# You can wait until the pool is at full-strength (providing an# optional timeout if desired), but it's not required before# calling the "apply_async" method. Jobs are queued until a# worker process is available.try:yieldpool.on_ready(timeout=10)exceptPoolTimeoutase:results=eelse:results=yieldgatherResults(map(pool.apply_async,calls,args))printresultstry:# You can gracefully close the pool, which ensures all jobs# already queued are completed before shutting down...yieldpool.close(timeout=10)exceptPoolTimeoutase:printe# ...or you can use force and immediately send SIGKILL to each# process in the pool.yieldpool.terminate(timeout=10)reactor.stop()reactor.callWhenRunning(main)reactor.run()