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()
相关推荐
qunshankeji7 小时前
交通事故自动识别_YOLO11分割_DRB实现
python
小呀小萝卜儿7 小时前
2025-11-14 学习记录--Python-特征归一化方法(Min-Max或StandardScaler)
开发语言·python·学习
顾安r7 小时前
11.14 脚本网页 青蛙过河
服务器·前端·python·游戏·html
测试19987 小时前
如何用Appium实现移动端UI自动化测试?
自动化测试·软件测试·python·测试工具·ui·职场和发展·appium
猿小猴子7 小时前
主流 AI IDE 之一的 Meituan CatPaw IDE 介绍
ide·meituan catpaw
Python私教7 小时前
第一个Python金融爬虫
爬虫·python·金融
蜡笔大新7988 小时前
IDEA中的异常
java·ide·intellij-idea
nvd119 小时前
Python 迭代器 (Iterator) vs. 生成器 (Generator)
开发语言·python
老罗-Mason9 小时前
Apache Flink运行环境搭建
python·flink·apache
Blossom.1189 小时前
大模型量化压缩实战:从FP16到INT4的生产级精度保持之路
开发语言·人工智能·python·深度学习·神经网络·目标检测·机器学习