python / python/cpython

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

Đang mở
#145,342 3 bình luận 2 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib topic-asyncio type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với BaseEventLoop._run_once(), API công khai được đề xuất và 12 bài kiểm thử của bản triển khai tham chiếu. Xem xét PR được liên kết gh-145343 và các công trình có trước về chế độ khách được dẫn chiếu; công việc được xem là hoàn tất khi chế độ khách được đề xuất hoạt động phối hợp với các vòng lặp sự kiện của máy chủ và vượt qua các bài kiểm thử của nó mà không gây hồi quy trong bộ kiểm thử asyncio.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.