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()
相关推荐
geng_zhaoying4 分钟前
在VPython中使用向量计算3D物体移动
python·3d·vpython
半tour费43 分钟前
TextCNN-NPU移植与性能优化实战
python·深度学习·分类·cnn·华为云
普通网友1 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
百锦再1 小时前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象
普通网友1 小时前
Python函数定义与调用:编写可重用代码的基石
jvm·数据库·python
龙泉寺天下行走1 小时前
Vscode 配置C++ Mingw调试、编译环境-无需修改系统PATH变量的VS Code配置方法
c++·ide·vscode
普通网友2 小时前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
MZ_ZXD0012 小时前
springboot流浪动物救助平台-计算机毕业设计源码08780
java·spring boot·后端·python·spring·flask·课程设计
十步杀一人_千里不留行2 小时前
解释器模式:为 LLM 构建迷你 DSL 解释器,实现 Prompt 编排语言
python·prompt·解释器模式
数据的世界012 小时前
Visual Studio 2026 正式发布:AI 原生 IDE 与性能革命的双向突破
ide·人工智能·visual studio