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()
相关推荐
梨落秋霜6 小时前
Python入门篇【文件处理】
android·java·python
Java 码农7 小时前
RabbitMQ集群部署方案及配置指南03
java·python·rabbitmq
张登杰踩8 小时前
VIA标注格式转Labelme标注格式
python
Learner8 小时前
Python数据类型(四):字典
python
odoo中国9 小时前
Odoo 19 模块结构概述
开发语言·python·module·odoo·核心组件·py文件按
Jelena157795857929 小时前
Java爬虫api接口测试
python
踩坑记录10 小时前
leetcode hot100 3.无重复字符的最长子串 medium 滑动窗口(双指针)
python·leetcode
诸神缄默不语11 小时前
Python处理Word文档完全指南:从基础到进阶
python
海棠AI实验室12 小时前
第四章 项目目录结构:src/、configs/、data/、tests/ 的黄金布局
python·项目目录结构
爱笑的眼睛1113 小时前
超越可视化:降维算法组件的深度解析与工程实践
java·人工智能·python·ai