我有一些使用tornadogen.coroutine
的异步函数,我通常将它们用作基于 tornado 的 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
更新:
一个更具体的例子:
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)
运行此输出:
myfunc
aaf