jupyter中使用使用事件循环

1、背景

应该在jupyter中遇到过这种问题:

"RuntimeError: This event loop is already running".

这表示,你在jupyter中使用事件循环会出现无法执行,因为他不允许嵌套,所以可以用下面方式解决。

2、方法

By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it's impossible to run tasks and wait for the result. Trying to do so will give the error "RuntimeError: This event loop is already running".

The issue pops up in various environments, such as web servers, GUI applications and in Jupyter notebooks.

This module patches asyncio to allow nested use of asyncio.run and loop.run_until_complete.

复制代码
pip3 install nest_asyncio

import nest_asyncio
nest_asyncio.apply()
相关推荐
魔尔助理顾问16 分钟前
Flask如何读取配置信息
python·flask·bootstrap
jc_hook1 小时前
Python 接入DeepSeek
python·大模型·deepseek
chicpopoo1 小时前
Python打卡DAY25
开发语言·python
crazyme_62 小时前
深入掌握 Python 切片操作:解锁数据处理的高效密码
开发语言·python
Code_流苏3 小时前
《Python星球日记》 第69天:生成式模型(GPT 系列)
python·gpt·深度学习·机器学习·自然语言处理·transformer·生成式模型
于壮士hoho4 小时前
Python | Dashboard制作
开发语言·python
掘金-我是哪吒4 小时前
分布式微服务系统架构第131集:fastapi-python
分布式·python·微服务·系统架构·fastapi
小猪快跑爱摄影5 小时前
【Folium】使用离线地图
python
keke105 小时前
Java【10_1】用户注册登录(面向过程与面向对象)
java·python·intellij-idea
微刻时光6 小时前
影刀RPA网页自动化总结
运维·人工智能·python·低代码·自动化·rpa·影刀rpa