Python教程

(Python)asyncio使用异常:This event loop is already running解决方式

本文主要是介绍(Python)asyncio使用异常:This event loop is already running解决方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

问题背景

asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持。最近我在多线程调用使用asyncio方法的时候,出现报错:

This event loop is already running

后来查过一些资料,发现asyncio不允许嵌套事件出现,需要引入nest_asyncio。

问题解决

引入nest_asyncio模块

pip install nest_asyncio -i https://pypi.douban.com/simple

代码添加

import nest_asyncio

nest_asyncio.apply()

其他的不需要改动。

总结

给自己留个记录,防止忘了。如果对你有用的话,点个赞吧

 

这篇关于(Python)asyncio使用异常:This event loop is already running解决方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!