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()
相关推荐
serve the people1 分钟前
python环境搭建 (十三) httpx和aiohttp
开发语言·python·httpx
Allen_LVyingbo2 分钟前
医疗AI新范式:当数理模型开始“计算”生命,传统大模型面临重构(中)
开发语言·人工智能·python·自然语言处理·重构·知识图谱
时艰.5 分钟前
Java 线程池 — ThreadPoolExecutor
java·开发语言·python
七夜zippoe7 分钟前
时间序列分析实战:从平稳性检验到Prophet与LSTM预测
人工智能·python·机器学习·arima·时间序列·prophet
多恩Stone12 分钟前
【3D-AICG 系列-2】Trellis 2 的O-voxel (上) Shape: Flexible Dual Grid
人工智能·python·算法·3d·aigc
AAD555888997 小时前
数字仪表LCD显示识别与读数:数字0-9、小数点及单位kwh检测识别实战
python
开源技术8 小时前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
Li emily9 小时前
解决港股实时行情数据 API 接入难题
人工智能·python·fastapi
wfeqhfxz258878210 小时前
农田杂草检测与识别系统基于YOLO11实现六种杂草自动识别_1
python
mftang10 小时前
Python 字符串拼接成字节详解
开发语言·python