gpt4 book ai didi

python - 选择器等同于 Python 2?

转载 作者:太空宇宙 更新时间:2023-11-04 05:20:51 27 4
gpt4 key购买 nike

Python 3 有 selectors 模块,它允许我们在文件描述符事件上注册,并且稍后在代码的不同部分 select 就可以了。

在 Python 2 中,我们只有 select 模块,它要求我们在同一行上“注册”和“选择”,这是有限制的。

如果我想在一个函数中注册一个事件并在另一个函数中等待它(“选择”),这在 Python 2 中如何完成?

最佳答案

所以有两个库可以充当这个角色:selectors2selectors34 .它们非常相似,支持 Python 2.6+,并且是选择器模块的直接替代品。但是,selectors2 集成了 PEP 475当它们被中断时重试系统调用并且有一个 more robust battery of tests以及所有主要操作系统(Windows、Mac OS 和 Linux)上的 100% 测试覆盖率和自动化测试。

安装模块:

python -m pip install selectors2

以下代码适用于 Python 2.6+

try:
import selectors
except ImportError:
import selectors2 as selectors

免责声明:我是 selectors2 的作者。

关于python - 选择器等同于 Python 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40392121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com