【问题标题】:How do I run Python asyncio code in a Jupyter notebook?如何在 Jupyter 笔记本中运行 Python asyncio 代码?
【发布时间】:2017-11-27 19:58:57
【问题描述】:

我有一些在 Python 解释器 (CPython 3.6.2) 中运行良好的异步代码。我现在想在带有 IPython 内核的 Jupyter notebook 中运行它。

我可以运行它

import asyncio
asyncio.get_event_loop().run_forever()

虽然这似乎可行,但它似乎也阻止了笔记本,并且似乎与笔记本不兼容。

我的理解是 Jupyter 在后台使用 Tornado,所以我尝试install a Tornado event loop as recommended in the Tornado docs

from tornado.platform.asyncio import AsyncIOMainLoop
AsyncIOMainLoop().install()

但是,这会产生以下错误:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-1-1139449343fc> in <module>()
      1 from tornado.platform.asyncio import AsyncIOMainLoop
----> 2 AsyncIOMainLoop().install()

~\AppData\Local\Continuum\Anaconda3\envs\numismatic\lib\site- packages\tornado\ioloop.py in install(self)
    179         `IOLoop` (e.g.,     :class:`tornado.httpclient.AsyncHTTPClient`).
    180         """
--> 181         assert not IOLoop.initialized()
    182         IOLoop._instance = self
    183 

AssertionError: 

终于找到了以下页面:http://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Asynchronous.html

所以我添加了一个包含以下代码的单元格:

import asyncio
from ipykernel.eventloops import register_integration

@register_integration('asyncio')
def loop_asyncio(kernel):
    '''Start a kernel with asyncio event loop support.'''
    loop = asyncio.get_event_loop()

    def kernel_handler():
        loop.call_soon(kernel.do_one_iteration)
        loop.call_later(kernel._poll_interval, kernel_handler)

    loop.call_soon(kernel_handler)
    try:
        if not loop.is_running():
            loop.run_forever()
    finally:
        loop.run_until_complete(loop.shutdown_asyncgens())
        loop.close()

在下一个单元格中我跑了:

%gui asyncio

这行得通,但我真的不明白它为什么以及如何工作。有人可以向我解释一下吗?

【问题讨论】:

    标签: python ipython-notebook jupyter python-asyncio


    【解决方案1】:

    2019 年 2 月 21 日编辑:问题已修复

    这不再是最新版本的 Jupyter Notebook 的问题。 Jupyter Notebook的作者详细介绍了这个案例here

    下面的答案是操作标记为正确的原始响应。


    这是很久以前发布的,但如果其他人正在寻找在 Jupyter Notebook 中运行异步代码的问题的解释和解决方案;

    Jupyter 的 Tornado 5.0 更新在添加了自己的 asyncio 事件循环后将 asyncio 功能变砖:

    因此,对于在 Jupyter Notebook 上运行的任何 asyncio 功能,您不能调用 loop.run_until_complete(...),因为您将从 asyncio.get_event_loop() 收到的循环将处于活动状态。

    相反,您必须将任务添加到当前事件循环中:

    import asyncio
    loop = asyncio.get_event_loop()
    loop.create_task(some_async_function())
    

    或者通过run_coroutine_threadsafe获取结果:

    import asyncio
    loop = asyncio.get_event_loop()
    asyncio.run_coroutine_threadsafe(some_async_function(), loop)
    

    【讨论】:

    • 这种行为有文档吗?
    • 真的很有帮助,谢谢。 + 上面有什么需要关闭的还是完整的?
    • @QHarr 如果关闭循环,那么 Jupyter 将停止工作。
    • 再次感谢您提供非常好的答案。
    • @GiorgioBalestrieri 更新帖子。
    【解决方案2】:

    在最新的 jupyter 版本中,这不再是问题!

    https://blog.jupyter.org/ipython-7-0-async-repl-a35ce050f7f7

    只需编写一个异步函数,然后直接在 jupyter 单元中等待它。

    async def fn():
      print('hello')
      await asyncio.sleep(1)
      print('world')
    
    await fn()
    

    【讨论】:

    • 感谢您提供应用命令,我在使用旧的解决方法时有点卡住了 :)
    【解决方案3】:

    我在 Jupyter 中使用 Asyncio 的瞬间是这样的:

    import time,asyncio
    
    async def count():
        print("count one")
        await asyncio.sleep(1)
        print("count four")
    
    async def count_further():
        print("count two")
        await asyncio.sleep(1)
        print("count five")
    
    async def count_even_further():
        print("count three")
        await asyncio.sleep(1)
        print("count six")
    
    async def main():
        await asyncio.gather(count(), count_further(), count_even_further())
    
    s = time.perf_counter()
    await main()
    elapsed = time.perf_counter() - s
    print(f"Script executed in {elapsed:0.2f} seconds.")
    

    输出:

    count one
    count two
    count three
    count four
    count five
    count six
    Script executed in 1.00 seconds.
    

    最初来自这里,但起初我并不清楚这个例子: https://realpython.com/async-io-python/

    【讨论】:

      【解决方案4】:

      我最近遇到了无法在 Jupyter 笔记本中运行异步代码的问题。这个问题在这里讨论:https://github.com/jupyter/notebook/issues/3397

      我尝试了讨论中的一种解决方案,到目前为止它解决了这个问题。

      pip3 install tornado==4.5.3

      这取代了默认安装的 tornado 版本 5.x。

      Jupyter 笔记本中的异步代码随后按预期运行。

      【讨论】:

      • 你能用异步代码重新运行 jupyter 单元吗?
      • 是的。到目前为止,一切都按预期工作。我不得不注释掉一些loop.close()
      • 那么,只有删除 loop.close 才能保证可重新运行?
      • 我没有资格说它是否保证可重复运行。它在我工作的特定环境中发挥了我预期的作用。降级到 tornado 4.5.3 后,RuntimeErrors 消失了。
      猜你喜欢
      • 2021-12-31
      • 2022-11-17
      • 1970-01-01
      • 2018-03-05
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      相关资源
      最近更新 更多