python / python/cpython

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

Open
#145,342 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-asyncio type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with BaseEventLoop._run_once(), the proposed public API, and the reference implementation's 12 tests. Review linked PR gh-145343 and the cited guest-mode prior art; done would mean the proposed guest mode works cooperatively with host event loops and passes its tests without regressions in the asyncio suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.