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()
相关推荐
吴佳浩1 小时前
Langchain 浅出
python·langchain·llm
smj2302_796826521 小时前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
mortimer1 小时前
破局视频翻译【最后一公里】––从语音克隆到口型对齐的完整工程思路
python·github·aigc
门框研究员4 小时前
解锁Python的强大能力:深入理解描述符
python
子不语1805 小时前
Python——函数
开发语言·python
daidaidaiyu5 小时前
一文入门 LangChain 开发
python·ai
JJ1M86 小时前
用 Python 快速搭建一个支持 HTTPS、CORS 和断点续传的文件服务器
服务器·python·https
汤姆yu6 小时前
基于python大数据的小说数据可视化及预测系统
大数据·python·信息可视化
x***J3486 小时前
Python多线程爬虫
开发语言·爬虫·python
m***D2867 小时前
Python网络爬虫实战案例
开发语言·爬虫·python