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()
相关推荐
万笑佛13 分钟前
Python 实现Kafka SASL认证生产消费
python
m0_617493942 小时前
Python OpenCV 透视变换(Perspective Transform)详解与实战
开发语言·python·opencv
小李不困还能学2 小时前
PyCharm下载安装与配置教程
ide·python·pycharm
博观而约取厚积而薄发2 小时前
Pytest 从入门到精通,一篇就够(超详细实战教程)
python·测试工具·单元测试·自动化·pytest
imzed2 小时前
使用 Playwright + Pytest 构建 Web UI 自动化测试框架
python·自动化·pytest
普通网友2 小时前
pytest一些常见的插件
开发语言·python·pytest
时空系2 小时前
Python 高性能高压缩打包器 —— 基于 JianPy 语义分析引擎
python
cndes3 小时前
给Miniconda换源,让包下载更迅速
开发语言·python
Metaphor6923 小时前
使用 Python 添加、隐藏和删除 PDF 图层
python·pdf·图层