在gatherResults完成后停止扭曲的反应堆

时间:2013-11-06 15:53:05

标签: 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()

我假设通过添加回调:

d.addCallback(lambda ignored: reactor.stop())

收集的结果实际上是对所有延期项添加了回调吗?

如果有,那么可能有一种更优雅/更正确的方法吗?

干杯!

1 个答案:

答案 0 :(得分:6)

  

我假设通过添加回调:       d.addCallback(lambda被忽略:reactor.stop())   收集到的结果实际上会在所有延期项目上添加回调吗?

事实并非如此。 gatherResults会返回 Deferred。它就像你可能遇到的任何其他Deferred一样。它的addCallback方法与平常做同样的事情:添加一个将在一个回调链中的一个点调用的函数。

一切都很好,有规律和非特殊的原因是gatherResults负责给予常规Deferred所需的所有逻辑,它返回结果在所有输入Deferred都有结果之后。

因此,您可以像使用任何其他gatherResults - 返回API一样使用Deferred。这不是特别的!

那就是说,从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, [])

请注意,您可以更改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

现在您的代码根本不需要任何twisted.internet.reactor次导入。

您还可以在twisted.internet.task.deferLater中使用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))