如何在Jupyter笔记本中运行Python asyncio代码?

snt*_*nth 22 python ipython-notebook python-asyncio jupyter

我有一些asyncio代码在Python解释器(CPython 3.6.2)中运行良好.我现在想在一个带有IPython内核的Jupyter笔记本中运行它.

我可以用它来运行它

import asyncio
asyncio.get_event_loop().run_forever()
Run Code Online (Sandbox Code Playgroud)

虽然这似乎工作,它似乎也阻止了笔记本电脑,似乎并没有与笔记本电脑很好玩.

我的理解是Jupyter使用了Tornado,所以我尝试按照Tornado文档中的建议安装Tornado事件循环:

from tornado.platform.asyncio import AsyncIOMainLoop
AsyncIOMainLoop().install()
Run Code Online (Sandbox Code Playgroud)

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

---------------------------------------------------------------------------
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: 
Run Code Online (Sandbox Code Playgroud)

最后我找到了以下页面: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()
Run Code Online (Sandbox Code Playgroud)

我在下一个单元格中运行:

%gui asyncio
Run Code Online (Sandbox Code Playgroud)

这有效,但我不明白为什么以及如何运作.有人可以向我解释一下吗?

Fel*_*ria 24

EDIT FEB 2019年第21期:问题已修复

这不再是最新版Jupyter Notebook的问题.Jupyter笔记本的作者详细的情况在这里.

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


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

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

<code>run_until_complete()</code>,因为您将从中接收的循环<code>asyncio.get_event_loop()</code>将处于活动状态.</p>

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

<pre><code>import asyncio
loop = asyncio.get_event_loop()
loop.create_task(some_async_function())
</code></pre><a target=Run Code Online (Sandbox Code Playgroud)

在Jupyter Notebook上运行的一个简单示例:

在此输入图像描述

  • @QHarr 如果关闭循环,则 Jupyter 将停止工作。 (2认同)
  • @FelipeFaria感谢您保持最新状态! (2认同)

小智 15

这在最新的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()
Run Code Online (Sandbox Code Playgroud)


Дми*_*вич 13

我在 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.")
Run Code Online (Sandbox Code Playgroud)

输出:

count one
count two
count three
count four
count five
count six
Script executed in 1.00 seconds.
Run Code Online (Sandbox Code Playgroud)

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


wav*_*per 10

只需使用%autoawait魔法命令,

例如:

%autoawait asyncio
Run Code Online (Sandbox Code Playgroud)
await some_function()
Run Code Online (Sandbox Code Playgroud)