Why stop when retrieve 403
coding=utf-8
from tornado import gen import tornado.ioloop import tornado.httpclient
@gen.coroutine
def rqbaidu():
url = 'http://iou99.tk'
http_cli = tornado.httpclient.AsyncHTTPClient()
resp = yield http_cli.fetch(url) #stop here
print resp
if resp.error:
print 'error'
else:
print resp.body + "----"
if name == 'main': f = rqbaidu() tornado.ioloop.IOLoop.instance().start()
As the code show above, this should print 'error' or resp.body, but this program stop at 'resp = yield http_cli.fetch(url) #stop here'
Why, a bug??
Coroutines (and Futures in general) hold on to any exceptions they generate so they can be raised in the calling function. If there is no calling function, you may never see the traceback. For batch-style work like this, you probably want to use IOLoop.run_sync(rqbaidu) instead of IOLoop.start(). You can also use IOLoop.add_future to attach a callback to the future so you can see when it fails (IOLoop.instance().add_future(lambda f: f.result()))