什么相当于扭曲的时刻(来自龙卷风)?

时间:2015-01-05 22:51:42

标签: twisted tornado

inlineCallbacks实施的

部分是:

        if isinstance(result, Deferred):
            # a deferred was yielded, get the result.
            def gotResult(r):
                if waiting[0]:
                    waiting[0] = False
                    waiting[1] = r
                else:
                    _inlineCallbacks(r, g, deferred)

            result.addBoth(gotResult)
            if waiting[0]:
                # Haven't called back yet, set flag so that we get reinvoked
                # and return from the loop
                waiting[0] = False
                return deferred

            result = waiting[1]
            # Reset waiting to initial values for next loop.  gotResult uses
            # waiting, but this isn't a problem because gotResult is only
            # executed once, and if it hasn't been executed yet, the return
            # branch above would have been taken.


            waiting[0] = True
            waiting[1] = None

如图所示,如果在inlineCallbacks - 装饰函数中,我会像这样打电话:

@inlineCallbacks
def myfunction(a, b):
    c = callsomething(a)
    yield twisted.internet.defer.succeed(None)
    print callsomething2(b, c)

此收益率将立即返回到该函数(这意味着:它不会被重新安排,但会立即从收益率继续)。这与Tornado的tornado.gen.moment形成鲜明对比(Future不会超过已经解决的None,结果为moment),这使得yielder重新安排自己,无论将来是什么解决与否。

我怎样才能像Tornado那样产生像{{1}}这样虚拟未来的行为?

1 个答案:

答案 0 :(得分:2)

等效可能类似于产生Deferred,直到“很快”才会触发。通常接受reactor.callLater(0, ...)来创建一个现在不运行但很快就会运行的定时事件。您可以使用Deferred轻松获得基于此的twisted.internet.task.deferLater(reactor, 0, lambda: None)

您可能希望查看备用计划工具(但在Twisted和Tornado中)。这种重新调度技巧通常只适用于小型,简单的应用程序。它的有效性减少了同时使用它的更多任务。

考虑twisted.internet.task.cooperate之类的内容是否可以提供更好的解决方案。