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()
相关推荐
诗句藏于尽头26 分钟前
Django模型与数据库表映射的两种方式
数据库·python·django
智数研析社35 分钟前
9120 部 TMDb 高分电影数据集 | 7 列全维度指标 (评分 / 热度 / 剧情)+API 权威源 | 电影趋势分析 / 推荐系统 / NLP 建模用
大数据·人工智能·python·深度学习·数据分析·数据集·数据清洗
扯淡的闲人39 分钟前
多语言编码Agent解决方案(5)-IntelliJ插件实现
开发语言·python
moxiaoran57531 小时前
Flask学习笔记(一)
后端·python·flask
CAE虚拟与现实1 小时前
VSCode中的下载VSIX是指什么?
ide·vscode·编辑器
秋氘渔1 小时前
迭代器和生成器的区别与联系
python·迭代器·生成器·可迭代对象
Gu_shiwww2 小时前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
Dxy12393102163 小时前
python把文件从一个文件复制到另一个文件夹
开发语言·python
sonrisa_4 小时前
collections模块
python
折翼的恶魔4 小时前
数据分析:排序
python·数据分析·pandas