gh-81554: Add add_reader support to ProactorEventLoop
Runs select.select in a background thread when add_reader/writer are called. Thread is terminated when the event loop is destroyed.
Solves a major problem with the default event loop lacking these methods (see #81554 for use cases).
Imports SelectorThread implementation to asyncio._selector_thread from Tornado 6.5.2 and uses it in ProactorEventLoop, fixing a longstanding compatibility problem with the default Windows event loop. Feel free to review _selector_thread, but note that it currently has almost no modifications to the original file from tornado (only removing some type hints not supported by the standard library).
Since this is only run on demand, no thread will be spawned for existing code that works on ProactorEventLoop, only new code that previously only worked on SelectorEventLoop or tornado's AddThreadEventLoop will now work with the default event loop.
It is possible to do this without a thread (trio does it), but it seems this is the simplest, most maintainable solution, as indicated in the discussion in #81554.
- Issue: gh-81554
📚 Documentation preview 📚: https://cpython-previews--141834.org.readthedocs.build/