如何在Jupyter笔记本中运行Python异步IO代码?

2024-05-07 04:12:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些在Python解释器(CPython3.6.2)中运行良好的异步IO代码。我现在想在一个带有IPython内核的Jupyter笔记本中运行这个

我可以用它运行

import asyncio
asyncio.get_event_loop().run_forever()

虽然这似乎是可行的,但它似乎也阻挡了笔记本电脑,而且似乎无法很好地使用笔记本电脑

我的理解是Jupyter在引擎盖下使用龙卷风,所以我试图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

这很管用,但我真的不明白它为什么管用,怎么管用。有人能给我解释一下吗


Tags: installrun代码infromimportloopevent
3条回答

编辑2019年2月21日:问题已解决

This is no longer an issue on the latest version of Jupyter Notebook. Authors of Jupyter Notebook detailed the case here.

下面的答案是由op标记为正确的原始回答


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

Jupyter的Tornado 5.0在添加了自己的异步IO事件循环后,更新了内置的异步IO功能:

Terminal output of <code>get_event_loop()</code>Jupyter Notebook output of <code>get_event_loop()</code>

因此,对于要在Jupyter笔记本上运行的任何异步IO功能,都不能调用run_until_complete(),因为从asyncio.get_event_loop()接收的循环将处于活动状态

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

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

Jupyter笔记本上运行的一个简单示例:

enter image description here

我在Jupyter中使用Asyncio的aha时刻如下所示:

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/

在最新的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()

相关问题 更多 >