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()
相关推荐
多米Domi0111 小时前
0x3f 第49天 面向实习的八股背诵第六天 过了一遍JVM的知识点,看了相关视频讲解JVM内存,垃圾清理,买了plus,稍微看了点确定一下方向
jvm·数据结构·python·算法·leetcode
人工智能训练6 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
yaoming1686 小时前
python性能优化方案研究
python·性能优化
码云数智-大飞7 小时前
使用 Python 高效提取 PDF 中的表格数据并导出为 TXT 或 Excel
python
VernonJsn8 小时前
visual studio 2022的windows驱动开发
ide·驱动开发·visual studio
阿尔帕兹8 小时前
Visual Studio2019/2022离线安装完整教程(含闪退解决方法)
ide·visual studio
萌止8 小时前
Visual Studio下载保姆级安装教程
ide·visual studio
GISer_Jing8 小时前
解决Visual Studio调试时找不到exe文件的终极指南
ide·visual studio
程序人生5188 小时前
Visual Studio 2026下载与安装详细教程!(附下载地址)
ide·visualstudio·visual studio
biuyyyxxx9 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化