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