asyncio: Add guest mode for running asyncio inside external event loops
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 BaseEventLoop._run_once()、提議的公開 API 以及參考實作的 12 個測試開始。審查連結的 PR gh-145343 和引用的訪客模式先例;完成的標準是,提議的訪客模式能夠與主機事件迴圈協同運作,並通過其測試,且不會在 asyncio 測試套件中引入回歸。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100