python / python/cpython

Extend `selectors` module to support platform-specific events

Open
#144,211 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the selectors module API, especially KqueueSelector, PollSelector, register(), modify(), and select(), then read select.kevent and the asyncio use case in Lib/asyncio/unix_events.py around lines 857-891. Determine how platform-specific events should be registered and represented by select(); done means a settled API design that supports the cited OS events without breaking the existing read/write contract.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.