Python Can';t调用futures tornado上的结果,总是结果对象
期望: 查看没有请求处理程序的结果。 不象Python Can';t调用futures tornado上的结果,总是结果对象,python,python-2.7,server,tornado,Python,Python 2.7,Server,Tornado,期望: 查看没有请求处理程序的结果。 不象 from somefolder import somelibrary @gen.coroutine def detailproduct(url): datafromlib=yield somelibrary(url) raise gen.Return(datafromlib) {“数据”: 我尝试这样的链接: "" 但不是工作 谁来帮帮我!tx在使用gen.coroutine时,您必须记住两条规则: gen.coroutine修饰
from somefolder import somelibrary
@gen.coroutine
def detailproduct(url):
datafromlib=yield somelibrary(url)
raise gen.Return(datafromlib)
{“数据”:
我尝试这样的链接:
""
但不是工作
谁来帮帮我!tx在使用
gen.coroutine
时,您必须记住两条规则:
gen.coroutine
修饰函数将自动返回未来gen.coroutine
修饰的协同程序,那么它也必须用gen.coroutine
修饰,并且必须使用yield
关键字来获得其结果detailproduct
用gen.coroutine
修饰-这意味着它将始终返回将来包装的datafromlib
如何修复 根据规则2,您必须使用
gen.coroutine
装饰调用方,并使用yield
关键字来获得未来的结果
{"data":<tornado.concurrent.future>}
或
您可以在将来设置一个回调函数,该函数在解析时将被调用。但这会让代码变得混乱
@gen.coroutine
def my_func():
data = yield detailproduct(url)
# do something with the data ...
def my_fun():
data_future = detailproduct(url)
data_future.add_done_callback(my_callback)
def my_callback(future):
data = future.result()
# do something with the data ...