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()
相关推荐
2501_944452233 小时前
字数统计 Cordova 与 OpenHarmony 混合开发实战
python
骚戴3 小时前
2025 Python AI 实战:零基础调用 LLM API 开发指南
人工智能·python·大模型·llm·api·ai gateway
kobe_OKOK_3 小时前
tdeinge REST API 客户端
python·缓存·django
io_T_T3 小时前
Python os库 os.walk使用(详细教程、带实践)
python
TonyLee0174 小时前
使用argparse模块以及shell脚本
python
Blossom.1185 小时前
Prompt工程与思维链优化实战:从零构建动态Few-Shot与CoT推理引擎
人工智能·分布式·python·智能手机·django·prompt·边缘计算
切糕师学AI5 小时前
Visual Studio 内存占用过高问题优化方案
ide·visual studio
love530love6 小时前
Windows 11 下 Z-Image-Turbo 完整部署与 Flash Attention 2.8.3 本地编译复盘
人工智能·windows·python·aigc·flash-attn·z-image·cuda加速
MediaTea7 小时前
Python:模块 __dict__ 详解
开发语言·前端·数据库·python
jarreyer7 小时前
python,numpy,pandas和matplotlib版本对应关系
python·numpy·pandas