Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 龙卷风执行令_Python_Tornado_Coroutine - Fatal编程技术网

Python 龙卷风执行令

Python 龙卷风执行令,python,tornado,coroutine,Python,Tornado,Coroutine,我试图理解并检查tornado是如何执行协同程序的。 我注意到一种行为,它让我觉得通用程序不起作用 看看下面的测试。它通过了,我希望得到l=[“S1”、“BF”、“S2”、“S3”],因为当子对象产生AsyncSubmin时,它返回到事件循环队列以拉取下一个回调,它应该得到“beforeYield”,因为它被安排得更早 def test_call_coroutine_function(ioLoop): l = [] def syncSubMain(): retu

我试图理解并检查tornado是如何执行协同程序的。 我注意到一种行为,它让我觉得通用程序不起作用

看看下面的测试。它通过了,我希望得到l=[“S1”、“BF”、“S2”、“S3”],因为当子对象产生AsyncSubmin时,它返回到事件循环队列以拉取下一个回调,它应该得到“beforeYield”,因为它被安排得更早

def test_call_coroutine_function(ioLoop):
    l = []

    def syncSubMain():
        return "Oh! at last I understood how tornado turns"

    @gen.coroutine
    def asyncSubMain():
        l.append("S2")
        return syncSubMain()

    def beforeYield():
        l.append("BF")

    @gen.coroutine
    def submain():
        l.append("S1")
        ioLoop.add_callback(beforeYield)
        y = yield asyncSubMain()            
        l.append("S3")
        raise gen.Return(y)

    @gen.coroutine
    def main():
        x = yield submain()
        raise gen.Return(x)

    assert ioLoop.run_sync(main).startswith("Oh!")
    assert l == ["S1", "S2", "S3", "BF"]
以下测试的行为符合我的要求,我不使用@gen.coroutine事件

def test_sync_all_async(ioLoop):
class C:
    f = 0
    l = []

    def mf(self):
        return 1

    # gen.coroutine is not needed and if just call a method
    # with gen_coroutine it's executed synchronously
    def m3(self, a):
        self.l.append(a)
        self.f = self.mf()

    def m2(self):
        self.l.append("A")
        ioLoop.add_callback(self.m3, "B")
        self.l.append("C")

    def m(self):
        self.m2()

c = C()
ioLoop.run_sync(c.m)
assert c.f == 1
assert c.l == ["A", "C", "B"]
我认为@gen.coroutine只是上述测试的语法糖。
在这些测试中,它要么不工作,要么与回调事件循环不同。

y=yield asyncSubmin()
正在挂起
submin
,等待调用结果(未来)得到解决。你的两个例子并不相同。谢谢。我还意识到我用函数而不是过程来举了一个错误的例子。什么样的方法可以检查事件循环的收益返回控制,而不仅仅是同步调用?我不是舒尔,真正知道同步或异步是协同路由。。。确切地说,只有当您真正切换上下文时,您的协同程序才会异步。为此,您必须使用yield执行yield gen.sleep(0)或任何其他异步操作。