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()
相关推荐
程序员:钧念5 小时前
深度学习与强化学习的区别
人工智能·python·深度学习·算法·transformer·rag
数据与后端架构提升之路6 小时前
TeleTron 源码揭秘:如何用适配器模式“无缝魔改” Megatron-Core?
人工智能·python·适配器模式
hele_two7 小时前
快速幂算法
c++·python·算法
l1t7 小时前
利用DeepSeek将python DLX求解数独程序格式化并改成3.x版本
开发语言·python·算法·数独
Cemtery1169 小时前
Day26 常见的降维算法
人工智能·python·算法·机器学习
星空椰10 小时前
快速掌握FastAPI:高效构建Web API
python·fastapi
塔尖尖儿10 小时前
Python中range()到底是什么演示
python
Ethan-D11 小时前
#每日一题19 回溯 + 全排列思想
java·开发语言·python·算法·leetcode
Echoo华地11 小时前
idea运行程序默认线程为daemon线程的问题
java·ide·intellij-idea