为什么 tornado AsyncHTTPClient 在 运行 来自 jupyter notebook 时表现不同?
Why does tornado AsyncHTTPClient behave differently when run from a jupyter notebook?
我正在尝试 运行 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))
它一直给我以下错误:
RuntimeError: IOLoop is already running
但是,如果我只是在 ipython 终端中 运行 它,那么它会 运行 如预期的那样。
知道为什么它不能从 jupyter-notebook 中 运行 吗?
我在 python3,tornado 版本 4.4.2
Jupyter内部使用的是Tornado,所以IOLoop.current()
指的是Jupyter已经启动的IOLoop
。使上述代码工作的最简单方法是创建一个新的 IOLoop:使用 loop = IOLoop()
而不是 loop = IOLoop.current()
。
我正在尝试 运行 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))
它一直给我以下错误:
RuntimeError: IOLoop is already running
但是,如果我只是在 ipython 终端中 运行 它,那么它会 运行 如预期的那样。
知道为什么它不能从 jupyter-notebook 中 运行 吗?
我在 python3,tornado 版本 4.4.2
Jupyter内部使用的是Tornado,所以IOLoop.current()
指的是Jupyter已经启动的IOLoop
。使上述代码工作的最简单方法是创建一个新的 IOLoop:使用 loop = IOLoop()
而不是 loop = IOLoop.current()
。