MagicStack / MagicStack/uvloop

Allow context for tasks that block the event loop

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

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

Ngôn ngữ chính
Cython
Star
11.9k
Fork
616
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hi all,

This is probably more of a discussion than an actual issue and I fully appreciate that this could be more of a Python asyncio question versus anything specific to uvloop's implementation (I've also started a discussion there), but I figured I'd ask here first because uvloop is wonderful and you all clearly have a deep understanding of creating an event loop implementation in Python.

My issue is this: when debug mode is enabled for the Python event loop, is it somehow possible to get slow logs to tell us more about the context the task is running in?

The motivation is that in many asyncio server frameworks like FastAPI, it is critical to not block the event loop but using asyncio's debug mode provides output that's not helpful.

Consider the following FastAPI server:

# server.py
# run with PYTHONASYNCIODEBUG=1 uvicorn server:app --reload --port 8000 --host 0.0.0.0
import time

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    time.sleep(2)  # intentionally block event loop for 2 seconds
    return {"message": "Hello World"}

If you curl http://127.0.0.1:8000/, you'll see something along the lines of:

Executing <Task finished name='Task-4' coro=<RequestResponseCycle.run_asgi() done, defined at /Users/mike.sukmanowsky/code/z/z/.venv/lib/python3.13/site-packages/uvicorn/protocols/http/h11_impl.py:401> result=None created at /Users/mike.sukmanowsky/code/z/z/.venv/lib/python3.13/site-packages/uvicorn/protocols/http/h11_impl.py:250> took 2.006 seconds

This is consistent with what uvloop and other event loop implementations do here.

But from this output I cannot determine:

  1. What endpoint this occurred on
  2. The full stack trace of my user-defined code that might point me to the culprit that's blocking the loop

For ASGI frameworks like FastAPI, blocking the event loop effectively means a death for concurrency.

I'm unaware of any way to make outputs more useful here, but I'm very much hoping either 1) I've missed an obvious way to do this or 2) this inspires some discussion that could lead to changes that'll eventually help all Python asyncio users better troubleshoot issues like this.

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

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

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 bằng cách đọc các dòng 268-273 trong uvloop/cbhandles.pyx và so sánh hành vi slow-log khi debug của nó với phần triển khai của asyncio, sử dụng bản tái hiện FastAPI trong server.py khi chạy dưới PYTHONASYNCIODEBUG=1. Issue này không định nghĩa đầu ra hoặc phần triển khai được chấp nhận, vì vậy hãy làm rõ ngữ cảnh mong muốn và hành vi của stack trace trước khi viết mã.

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, observability
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
Cần làm rõ
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.