classification
Title: Allow to create new Event Loops on Threads
Type: behavior Stage: patch review
Components: asyncio Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Christian Exposito, asvetlov, christian.heimes, python-dev, yselivanov
Priority: normal Keywords: patch

Created on 2020-05-28 17:54 by Christian Exposito, last changed 2020-05-28 18:55 by christian.heimes.

Pull Requests
URL Status Linked Edit
PR 20500 open python-dev, 2020-05-28 17:56
Messages (2)
msg370257 - (view) Author: Christian Exposito (Christian Exposito) * Date: 2020-05-28 17:54
Right now, Async IO module is not automatically creating a new event loop on threads that are not the main thread (https://github.com/python/cpython/blob/master/Lib/asyncio/events.py#L638), but it should be interesting to do it. For example, WSGI applications handle web requests by spawning a new thread. If we allow Async IO module to create event loops on those threads, we will improve its adoption for developing web applications.
msg370261 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2020-05-28 18:55
The default policy of asyncio deliberately limits automatic creation of event loops to the main thread. You can't just mix asyncio with a threaded web application and expect it to work correctly.

See https://github.com/python/cpython/blob/a487a39dca4c41305928c7dfdbcb0b3aa344683b/Lib/asyncio/events.py#L609-L620

    In this policy, each thread has its own event loop.  However, we
    only automatically create an event loop by default for the main
    thread; other threads by default have no event loop.

    Other policies may have different rules (e.g. a single global
    event loop, or automatically creating an event loop per thread, or
    using some other notion of context to which an event loop is
    associated).
History
Date User Action Args
2020-05-28 18:55:11christian.heimessetnosy: + christian.heimes
messages: + msg370261
2020-05-28 17:56:26python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request19748
stage: patch review
2020-05-28 17:54:39Christian Expositocreate