asyncio: Add guest mode for running asyncio inside external event loops
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Feature request
Add guest mode to asyncio, enabling it to run cooperatively inside a host event loop such as Tkinter, Qt, GTK, or any other framework that owns the main thread.
Motivation
GUI toolkits (Tk, Qt, GTK, pygame, ...) each have their own event loop that must run on the main thread. Today there is no standard way to use asyncio coroutines inside a GUI application without:
- Polling hacks (periodic
root.after()calls) - Replacing the event loop entirely (fragile, framework-specific)
- Running asyncio in a separate thread and manually marshalling results
Trio solved this problem with trio.lowlevel.start_guest_run() and the approach has proven robust across many GUI frameworks (see trio-guest).
Proposed API
import asyncio
task = asyncio.start_guest_run(
async_fn, *args,
run_sync_soon_threadsafe=host.schedule_callback,
done_callback=host.on_done,
)
The implementation decomposes BaseEventLoop._run_once() into three new public methods — poll_events(), process_events(), and process_ready() — then uses a dual-thread architecture where the host thread processes callbacks and a daemon I/O thread polls for events.
Prior art and discussion
- Trio guest mode
- trio-guest — multi-framework reference implementation
- asyncio-guest — proof-of-concept for asyncio (this proposal is derived from it)
- discuss.python.org: Connecting asyncio and tkinter event loops
A reference implementation with tests (12 tests passing, full asyncio test suite regression-free) is ready to submit as a PR.
Linked PRs
- gh-145343
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit BaseEventLoop._run_once(), der vorgeschlagenen öffentlichen API und den 12 Tests der Referenzimplementierung. Prüfe den verknüpften PR gh-145343 und die zitierte Vorarbeit zum Gastmodus; als abgeschlossen gilt die Arbeit, wenn der vorgeschlagene Gastmodus kooperativ mit Host-Ereignisschleifen funktioniert und seine Tests ohne Regressionen in der asyncio-Suite besteht.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100