asyncio: Add guest mode for running asyncio inside external event loops
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia da BaseEventLoop._run_once(), dall’API pubblica proposta e dai 12 test dell’implementazione di riferimento. Esamina la PR collegata gh-145343 e i precedenti citati sulla modalità ospite; il lavoro sarà considerato completato quando la modalità ospite proposta funzionerà in modo cooperativo con i loop di eventi dell’host e supererà i relativi test senza regressioni nella suite di asyncio.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100