python / python/cpython

Extend `selectors` module to support platform-specific events

未关闭
#144,211 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

The selectors module API is deliberately very narrow:

  • The register() / modify() methods only accept events as a bitmask of EVENT_READ | EVENT_WRITE
  • The select() method only ever returns combinations of EVENT_READ | EVENT_WRITE

This design makes it impossible to use many platform-specific events provided by the underlying OS APIs (poll, epoll, kqueue). For example, https://github.com/python/cpython/issues/79698 describes a use case for EPOLLEXCLUSIVE on Linux. The biggest missed opportunity, however, is the inability to fully exploit kqueue() on BSD / macOS, which would allow async handling of regular files, signals, and others (see KQ_EVENT_*).

This limitation is relevant for asyncio, which uses the selectors module. For instance, on BSD / macOS, asyncio could be notified when a process terminates, enabling an optimization similar to https://github.com/python/cpython/pull/144047. Currenly asyncio on Linux already takes advantage of this approach via os.pidfd_open() + selectors.PollSelector (see source), but can't do the same for BSD / macOS because selectors.KqueueSelector doesn't allow registering custom select.kevent() objects.

Whereas it would be relatively easy to add a register_kevent() method, or to pass EPOLL* events directly to register(), it's less clear how the select() method should behave in this case, since it's apparently bound to the promise of returning either EVENT_READ or EVENT_WRITE.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 selectors 模块 API 入手,重点查看 KqueueSelector、PollSelector、register()、modify() 和 select(),然后阅读 select.kevent 以及 Lib/asyncio/unix_events.py 中 857-891 行附近的 asyncio 使用场景。确定平台特定事件应如何注册以及如何由 select() 表示;完成的标准是形成一个确定的 API 设计,在不破坏现有 read/write 契约的情况下支持所引用的 OS 事件。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。