python - Python 2 的选择器等效吗?

标签 python networking standard-library

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 选择器2

以下代码适用于 Python 2.6+

try:
    import selectors
except ImportError:
    import selectors2 as selectors

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

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

相关文章:

python - httpretty.enable() 之后 MongoClient 连接失败

python - Python3 中自动定义的数值运算符

azure - 是否可以使用 Databricks 的专用终结点连接到 Azure 存储帐户?

c++ - 如何将 std::vector 的某些元素移动到 vector 中的新索引?

c - 转换说明符如何工作?

python - 获取易趣 OAuth token

python - 将带有时区的日期格式更改为 yyyy-mm-dd hh :mm:ss

Angular 2 本地主机和后端服务器连接

java - Netty SslHandler 抛出 IllegalReferenceCountException

c - fwprintf 不输出宽字符