新版本get_event_loop过时

来源:8-6 自动化测试开发之使用mysql异步连接池实现mysql数据库操作(下)

满满是我就对了

2022-07-28

老师,最新版本的python asyncio中的get_event_loop方法已被列为过时方法,官方推荐用其他的方法改造。现在这个database类仍正常运行但是会出三个warning。请问老师该如何改造代码使得其满足最新的版本呢

官方信息:
Get the current event loop.
If there is no current event loop set in the current OS thread, the OS thread is main, and set_event_loop() has not yet been called, asyncio will create a new event loop and set it as the current one.
Because this function has rather complex behavior (especially when custom event loop policies are in use), using the get_running_loop() function is preferred to get_event_loop() in coroutines and callbacks.
Consider also using the asyncio.run() function instead of using lower level functions to manually create and close an event loop.
Deprecated since version 3.10: Deprecation warning is emitted if there is no running event loop. In future Python releases, this function will be an alias of get_running_loop().

写回答

1回答

托尼老师

2022-07-28

从官方文档的说明来看,在将来的版本中,get_event_loop方法会成为get_running_loop方法的别名,目前之所以出现弃用的警告,是新版本python中,不推荐复杂实现方式的get_event_loop,而推荐使用get_running_loop。

所以,你可以将get_event_loop换成new_event_loop,或者,直接重构读写数据库的异步代码,然后调用asyncio.run()接口。

0
0

Python自动化测试开发实战,能帮你就业的测试课

开发高扩展、高可用的自动化测试框架,利用好测试自动化技术。

445 学习 · 157 问题

查看课程