我有一些asyncio代码在Python解释器(CPython 3.6.2)中运行良好。我现在想在一个带有IPython内核的Jupyter笔记本中运行它。
我可以用它来运行它
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
这有效,但我不明白为什么以及如何运作。有人可以向我解释一下吗?
EDIT FEB 2019年第21期:问题已修复
这不再是最新版Jupyter Notebook的问题。 Jupyter笔记本的作者详细介绍了here案例。
下面的答案是操作标记正确的原始响应。
这是在很久以前发布的,但是如果其他人正在寻找解决Jupyter Notebook中运行异步代码问题的解释和解决方案;
添加自己的asyncio事件循环后,Jupyter的Tornado 5.0更新了asyncio功能:
因此,对于在Jupyter Notebook上运行的任何asyncio功能,您无法调用run_until_complete()
,因为您将从asyncio.get_event_loop()
收到的循环将处于活动状态。
相反,您必须将任务添加到当前循环:
import asyncio
loop = asyncio.get_event_loop()
loop.create_task(some_async_function())
在Jupyter Notebook上运行的一个简单示例:
这在最新的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()
我最近遇到了无法在Jupyter笔记本中运行asyncio代码的问题。这里讨论的问题是:https://github.com/jupyter/notebook/issues/3397
我在讨论中尝试了其中一个解决方案,到目前为止它解决了这个问题。
pip3 install tornado==4.5.3
这取代了默认安装的tornado版本5.x.
然后,Jupyter笔记本中的asyncio代码按预期运行。
我在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/