简体   繁体   中英

Why does tornado AsyncHTTPClient behave differently when run from a jupyter notebook?

I was trying to run the following piece of code on jupyter-notebook

from tornado.httpclient import AsyncHTTPClient
from tornado.ioloop import IOLoop

async def fetch_coroutine(url):
    http_client = AsyncHTTPClient()
    response = await http_client.fetch(url)
    return response.body

url = 'http://www.tornadoweb.org/en/stable/'

loop = IOLoop.current()
loop.run_sync(lambda : fetch_coroutine(url))

and it keeps giving me the following error:

RuntimeError: IOLoop is already running

However if I simply run it in an ipython terminal then it runs as expected.

Any idea why it won't run from within a jupyter-notebook?

I'm on python3, tornado version 4.4.2

Jupyter uses Tornado internally, so IOLoop.current() refers to the IOLoop that Jupyter has already started. The simplest way to make the above code work is to create a new IOLoop: use loop = IOLoop() instead of loop = IOLoop.current() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM