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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java出现编译错误,我不理解   java在gnu-trove库中是否有任何有序映射?   java Servlet应该有映射,但找不到Servlet   java时间和第二期显示1:10,而不是13:10   java Play Framework 1.2.7 Heroku更新崩溃   线程“main”java中的opencsv异常。lang.NoClassDefFoundError:org/apache/commons/lang3/ObjectUtils   selenium在java中隐藏警告消息   java使用ID引用将JSON实体反序列化为POJO   java无法在JRE 8中加载字体   一个线程中的异常/错误会使整个应用程序停止吗?   java访问重复子规则的元素标签;e、 g.用ANTLR解析(1,2,3)中的a   java如何从平移旋转中找到新坐标   使用HTML Java小程序托管jar文件存在安全问题   java如何按频率而不是字母顺序排列字符串数组   java清除bufferedReader和块以获得更多输入   java解密SAML2断言