12
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tornado >= 5以上がインストールされたjupyter-notebook上でevent_loopを扱うとRuntimeError: This event loop is already runningになる

Last updated at Posted at 2018-09-10

問題

jupyte-notebook を動かしているサーバではフレームワークに tornado を使っている。

tornado >= 5 のバージョンがインストールされた jupyter-notebook 上では、 asyncio モジュールの event_loop を扱うと RuntimeError となる。

GitHub | jupyter/notebook | Can't invoke asyncio event_loop after tornado 5.0 update #3397

動かなかったサンプル

pyppeteer を試そうとしたら遭遇した。。普通のREPL上では動くけどjupyter-notebook 上では動かない。

import asyncio
from pyppeteer import launch

loop = asyncio.get_event_loop()

async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.goto('http://example.com')
    await page.screenshot({'path': 'example.png'})
    await browser.close()


loop.run_until_complete(main())

結果

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-b0a5d64b1fb0> in <module>()
----> 1 loop.run_until_complete(main())

~/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
    452         future.add_done_callback(_run_until_complete_cb)
    453         try:
--> 454             self.run_forever()
    455         except:
    456             if new_task and future.done() and not future.cancelled():

~/.anyenv/envs/pyenv/versions/3.6.4/lib/python3.6/asyncio/base_events.py in run_forever(self)
    406         self._check_closed()
    407         if self.is_running():
--> 408             raise RuntimeError('This event loop is already running')
    409         if events._get_running_loop() is not None:
    410             raise RuntimeError(

RuntimeError: This event loop is already running

解決策

If I specify tornado==4.5.3 before pip install jupyter, it works fine

とのこと

$ pip install tornado<=4.5.3
$ pip install jupyter

jupyterrequirements.txtではtornadoのバージョンは4以上であればよい、となっているので、更新はかからない。

原因

issue見る限り以下が原因だけど、よく分かっていないです。誰かご解説を。。

What’s new in Tornado 5.0 | backwards-compatibility-notes

"On Python 3, IOLoop is always a wrapper around the asyncio event loop."

12
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?