You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Decompose asyncio.run_forever() into parts to improve integration with other event loops #110771
This is an admittedly niche use case, but it's one that we've hit in the development of Toga.
GUI toolkits all have event loops. If you want to use Python asyncio calls in a GUI app, it is necessary to make the GUI's event loop and Python's asyncio event loop co-exist.
This usually means writing a subclass of EventLoop that overrides run_forever() in some way. Either the inner while True: _run_once() loop of run_forever() needs to be modified to integrate the equivalent "run once" of the GUI's event loop; or the while True loop needs to be subverted entirely and _run_once() needs to be invoked regularly on the GUI's event loop.
However, the implementation of run_forever() makes this difficult to do without duplicating significant portions of the implementation of run_forever(). For example, Toga's Winforms asyncio integration involves duplicating the startup and shutdown of run_forever()
I propose that the implemenation of run_forever() be decomposed into exactly these parts - an initial setup, the actual loop, and a post-loop cleanup. This does not impact on the operation of ascynio itself, but it would allow Toga (and any other integrated asyncio loop) to automatically inherit any improvements that are made to the core event loop.
It is worth noting that CPython's own Winforms integration has a similar need. The implementation of run_forever() in ProactorEventLoop() needs to include additional setup and additional cleanup calls. Decomposing the base event loop would allow this additional setup and cleanup to be explcit.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
Informal in-person discussion at CPython core team sprint.
Thanks! I think this (GUI event loop integration) was an originally intended use case, and there was talk of doing a Tkinter integration as an example, but it never panned out, and I stopped caring about Tkinter personally.
It was my understanding that most GUI integrations just run the asyncio event loop in a separate thread. I think it's exciting that Toga managed to do this integration right and that a feature request came out of it.
Could you propose a specific API? It shouldn't be hard to get rough consensus on that, and then you can start implementing. Or you could start with an implementation (so you're perhaps more sure that it actually works, etc.). Up to you.
I stumbled over this patch, because it broke the integration in PyGObject (i.e. GLib/GTK) unfortunately. But, the fix is simple enough and there is less code duplication after the change, so I am happy with it anyway. See https://gitlab.gnome.org/GNOME/pygobject/-/merge_requests/502
It was my understanding that most GUI integrations just run the asyncio event loop in a separate thread. I think it's exciting that Toga managed to do this integration right and that a feature request came out of it.
In upstream GLib/pygobject, we added a tight event loop integration that builds on top of the existing SelectorEventLoop/ProactorEventLoop classes. Effectively, this transparently runs the normal python event loop as a child of the GLib main loop (i.e. main context). Which on the technical side works by implementing a custom GSource (for both Unix and Windows). The somewhat tricky part is that run_forever needs to be replaced. The python EventLoop is then only dispatched from the internal GSource when GLib decides, i.e. it directly calls into _run_once.
Implementing the custom GSource requires some trickery for the Selector obviously. On Windows, hooking into the IocpProactor is actually more straight forward even.
Activity
gvanrossum commentedon Oct 12, 2023
Thanks! I think this (GUI event loop integration) was an originally intended use case, and there was talk of doing a Tkinter integration as an example, but it never panned out, and I stopped caring about Tkinter personally.
It was my understanding that most GUI integrations just run the asyncio event loop in a separate thread. I think it's exciting that Toga managed to do this integration right and that a feature request came out of it.
Could you propose a specific API? It shouldn't be hard to get rough consensus on that, and then you can start implementing. Or you could start with an implementation (so you're perhaps more sure that it actually works, etc.). Up to you.
gh-110771: Decompose run_forever() into parts (#110773)
pythongh-110771: Decompose run_forever() into parts (python#110773)
pythongh-110771: Decompose run_forever() into parts (python#110773)
events: Try calling _run_forever_{setup,cleanup}
events: Try calling _run_forever_{setup,cleanup}
events: Try calling _run_forever_{setup,cleanup}
events: Try calling _run_forever_{setup,cleanup}
benzea commentedon Jan 13, 2026
I stumbled over this patch, because it broke the integration in PyGObject (i.e. GLib/GTK) unfortunately. But, the fix is simple enough and there is less code duplication after the change, so I am happy with it anyway. See https://gitlab.gnome.org/GNOME/pygobject/-/merge_requests/502
In upstream GLib/pygobject, we added a tight event loop integration that builds on top of the existing SelectorEventLoop/ProactorEventLoop classes. Effectively, this transparently runs the normal python event loop as a child of the GLib main loop (i.e. main context). Which on the technical side works by implementing a custom GSource (for both Unix and Windows). The somewhat tricky part is that
run_foreverneeds to be replaced. The python EventLoop is then only dispatched from the internal GSource when GLib decides, i.e. it directly calls into_run_once.Implementing the custom GSource requires some trickery for the Selector obviously. On Windows, hooking into the IocpProactor is actually more straight forward even.