在常规python脚本中使用tornado异步代码

rak*_*ice 6 python asynchronous tornado coroutine

我有一些使用龙卷风的gen.coroutine异步函数,我通常将其用作基于龙卷风的Web应用程序的一部分.但是,我想从一个普通的旧python脚本中调用它们中的一些来执行一些管理任务.我该怎么做呢?

from tornado import gen

import some_internal_stuff

@gen.coroutine
def myfunc(x):
    y = yield some_internal_stuff.another_async_func(x)
    raise gen.Return(y)

if __name__ == "__main__":
    # What do I put here to call myfunc(1) and get the async return value?
    pass
Run Code Online (Sandbox Code Playgroud)

更新:

一个更具体的例子:

from tornado import gen

@gen.coroutine
def another_async_func(x):
    print "aaf"
    raise gen.Return(x + 1)

@gen.coroutine
def myfunc(x):
    print "myfunc"
    y = yield another_async_func(x)
    print "back"
    raise gen.Return(y)

def callback(y):
    print "Callback called with %d" % y

if __name__ == "__main__":
    myfunc(1, callback=callback)
Run Code Online (Sandbox Code Playgroud)

运行此输出:

myfunc
aaf
Run Code Online (Sandbox Code Playgroud)

rak*_*ice 17

有一个内置的方法run_syncIOLoop运行一个单一的呼叫,然后停止循环,所以它是非常容易的,只是添加事件循环,只要你有在PYTHONPATH龙卷风纯python脚本.

通过具体的例子:

from tornado import gen, ioloop

@gen.coroutine
def another_async_func(x):
    print "aaf"
    raise gen.Return(x + 1)

@gen.coroutine
def myfunc(x):
    print "myfunc"
    y = yield another_async_func(x)
    print "back"
    raise gen.Return(y)

@gen.coroutine
def main():
    y = yield myfunc(1)
    print "Callback called with %d" % y

if __name__ == "__main__":
    ioloop.IOLoop.instance().run_sync(main)
Run Code Online (Sandbox Code Playgroud)

这输出:

myfunc
aaf
back
Callback called with 2
Run Code Online (Sandbox Code Playgroud)

注意run_sync不能很好地嵌套; 如果你run_sync在一个被调用的函数中调用run_sync同一个IOLoop内部调用的完成将停止,IOLoop并且yield在内部调用之后将不再返回s.