python / python/cpython

asyncio: Add guest mode for running asyncio inside external event loops

オープン
#145,342 コメント 3 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib topic-asyncio type-feature
主要言語
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

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

BaseEventLoop._run_once()、提案されている公開 API、リファレンス実装の 12 個のテストから始めてください。リンクされている PR gh-145343 と、引用されているゲストモードの先行事例を確認してください。完了の条件は、提案されたゲストモードがホストのイベントループと協調して動作し、asyncio スイートにリグレッションを起こさずにそのテストに合格することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。