tornado icon indicating copy to clipboard operation
tornado copied to clipboard

Why stop when retrieve 403

Open leon4py opened this issue 13 years ago • 1 comments

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??

leon4py avatar Oct 22 '13 03:10 leon4py

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()))

bdarnell avatar Oct 22 '13 16:10 bdarnell