python - Jupyter notebook 运行等待功能
问题描述
在 Jupyter notebook 上学习协程和任务后,
运行以下代码
import asyncio
async def main():
print('learn')
await asyncio.sleep(1)
print('Jupyter')
但是,它在 Ipython 上可以正常工作
解决方案
这是更高版本的 Jupyter的一个已知问题。安装nest_asyncio
作为解决方法。
> pip install nest_asyncio
代码
import asyncio
import nest_asyncio
nest_asyncio.apply()
async def main():
print("Learn")
await asyncio.sleep(1)
print("Jupyter")
asyncio.run(main())
# 'Learn'
# 'Jupyter'
TLDR ; 在笔记本中运行 asyncio 与 Tornado 5.0 在后台运行的现有事件循环冲突。第二种选择是降级notebook
到依赖于旧版本 Tornado 的版本。
推荐阅读
- java - 生成的 servlet 错误:-source 1.5 不支持默认方法(使用 -source 8 或更高版本启用默认方法)
- django - 当我在 ajax 请求中使用 set_password 时引发 ConnectionResetError
- react-native - 如何在本机反应中将数据从下一个屏幕发送到上一个屏幕并将数据附加到上一个屏幕状态变量的数组中
- asp.net-core - Razor 页面中的流利验证
- laravel-5 - 使用 laravel 将数组存储在 db 中的数组中
- python - Python:如何使用空格创建自定义 CLI 命令?
- c - 将 2 个 ctype.h 函数组合成 C 中的 IF
- sql - 查找 SQL Server 中多次注册的最大连续时间段
- scala - 未解决的依赖项 — com.typesafe.play#sbt-plugin;2.4.3:未找到
- android - Android Studio 中的 getSharedPreferences(名称:“数据库”)错误