2

新来的扭曲,只是尝试一些延迟的东西。我有以下代码组成了 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())

收集到的结果实际上在所有延迟项目上添加了回调?

如果是这样,那么可能有更优雅/正确的方法来做到这一点?

干杯!

4

1 回答 1

7

我假设通过向收集的结果添加回调: d.addCallback(lambda ignored: reactor.stop()) 实际上在所有延迟项目上添加了该回调?

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

像这样一切都很好、规则和不特殊的原因是,gatherResults它处理了所有必要的逻辑,只在所有 input 都有结果之后给出它返回结果的规则。DeferredDeferred

因此,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, [])

请注意,您可以进行更改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.deferLateringetDummyData来节省更多输入:

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))
于 2013-11-06T17:22:08.823 回答