Tra*_*lie 1 python twisted deferred
新的扭曲,只是尝试一些延期的东西.我有以下代码组成100个延迟调用的列表 - 每个等待一个随机时间并返回一个值.该列表打印结果并最终终止反应器.
但是,我很确定我停止反应堆的方式可能......不太好.
__author__ = 'Charlie'
from twisted.internet import defer, reactor
import random
def getDummyData(x):
"""returns a deferred object that will have a value in some random seconds
sets up a callLater on the reactor to trgger the callback of d"""
d = defer.Deferred()
pause = random.randint(1,10)
reactor.callLater(pause, d.callback, (x, pause))
return d
def printData(result):
"""prints whatever is passed to it"""
print result
def main():
"""makes a collection of deffered calls and then fires them. Stops reactor at end"""
deferred_calls = [getDummyData(r) for r in range(0,100)]
d = defer.gatherResults(deferred_calls, consumeErrors = True)
d.addCallback(printData)
# this additional callback on d stops the reacor
# it fires after all the delayed callbacks have printed their values
# the lambda ignored: ractor.stop() is required as callback takes a function
# that takes a parameter.
d.addCallback(lambda ignored: reactor.stop())
# start the reactor.
reactor.run()
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
我假设通过添加回调:
d.addCallback(lambda ignored: reactor.stop())
Run Code Online (Sandbox Code Playgroud)
对收集的结果实际上添加了所有延期项目的回调?
如果是这样,那么可能有更优雅/正确的方法吗?
干杯!
我假设通过添加回调:d.addCallback(lambda ignored:reactor.stop())到收集的结果实际上在所有延期项目上添加了回调?
事实并非如此. gatherResults返回一个新的 Deferred.就像Deferred你遇到的任何其他人一样.它的addCallback方法与平常一样:添加一个将在一个回调链中的一个点调用的函数.
在一切的原因很不错,经常和unspecial像这是一个gatherResults需要照顾所有必要的逻辑来只给有规律Deferred,它毕竟输入返回结果Deferred■找的结果.
因此,请随意使用gatherResults,就像您使用任何其他Deferred令人遗憾的API一样.这不是特别的!
也就是说,从Twisted 12.3开始,有一个方便的实用程序,你可能想要用于这种事情 - twisted.internet.task.react.以下是main您使用它时的功能:
def main(reactor):
"""makes a collection of deffered calls and then fires them. Stops reactor at end"""
deferred_calls = [getDummyData(r) for r in range(0,100)]
d = defer.gatherResults(deferred_calls, consumeErrors = True)
d.addCallback(printData)
return d
if __name__ == "__main__":
from twisted.internet import task
task.react(main, [])
Run Code Online (Sandbox Code Playgroud)
请注意,您可以更改,getDummyData以便它不依赖于全局反应器:
def getDummyData(reactor, x):
"""returns a deferred object that will have a value in some random seconds
sets up a callLater on the reactor to trgger the callback of d"""
d = defer.Deferred()
pause = random.randint(1,10)
reactor.callLater(pause, d.callback, (x, pause))
return d
def main(reactor):
"""makes a collection of deffered calls and then fires them. Stops reactor at end"""
deferred_calls = [getDummyData(reactor, r) for r in range(0,100)]
d = defer.gatherResults(deferred_calls, consumeErrors = True)
d.addCallback(printData)
return d
Run Code Online (Sandbox Code Playgroud)
现在你的代码根本不需要任何twisted.internet.reactor导入.
您还可以使用twisted.internet.task.deferLaterin getDummyData来节省更多的输入:
def getDummyData(reactor, x):
"""returns a deferred object that will have a value in some random seconds
sets up a callLater on the reactor to trgger the callback of d"""
pause = random.randint(1,10)
return deferLater(reactor, pause, lambda: (x, pause))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1633 次 |
| 最近记录: |