我想知道为什么在这个函数中:
@tornado.gen.engine
def check_status_changes(netid, sensid):
como_url = "".join(['http://131.114.52:44444/ztc?netid=', str(netid), \
'&sensid=', str(sensid), '&start=-5s&end=-1s'])
http_client = AsyncHTTPClient()
response = yield tornado.gen.Task(http_client.fetch, como_url)
if response.error:
raise Exception(response.error)
当有response.error时,我获得了标题错误...为了在另一个函数中捕获返回值,我必须得到什么?
我会做以下事情:
try:
periodic = tornado.ioloop.PeriodicCallback(check_status_changes(netid, sensid), 5000)
value = periodic.start()
print("Secondo")
print value
except:
print("Quarto")
periodic.stop()
self.finish()
return
else:
我不知道......我只是将返回值与另一个值进行比较....
谢谢。
答案 0 :(得分:4)
您可以使用
raise tornado.gen.Return(response)
答案 1 :(得分:3)
该函数有一个gen.engine
装饰器,你不能从里面返回一个值(不是龙卷风相关,你不能在生成器内返回一个值)。
如果你试图从该函数中获取一个值 - 提供的是你在IOLoop上调用它,那么该函数应该有一个callback
(可调用的)关键字参数:
@tornado.gen.engine
def check_status_changes(netid, sensid, callback=None):
response = yield tornado.gen.Task(do_your_thing)
if response.error:
raise response.error
callback(response.body) # this is how you make the value available to the
# caller; response.body is HTTPResponse specific iirc
现在你可以在其他地方调用这个函数:
# somewhere in a gen.engine decorated async method
body = yield tornado.gen.Task(check_status_changes, netid, sensid)
答案 2 :(得分:0)
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
http_client = AsyncHTTPClient()
http_client = tornado.httpclient.AsyncHTTPClient()
response = yield http_client.fetch('http://localhost:1338/api/getDistinctGeoPositions/?durationInMinutes=9000')
if response.error:
print response.error
self.finish(response.body)