MagicStack / MagicStack/uvloop

Allow context for tasks that block the event loop

未关闭
#656 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Cython
星标
11.9k
派生
616
PR 合并指标
30 天内没有已合并 PR

描述

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](https://discuss.python.org/t/improve-the-context-of-asyncio-slow-log-reporting/83225)), 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:

```python
# 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 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](https://github.com/MagicStack/uvloop/blob/7bb12a174884b2ec8b3162a08564e5fb8a5c6b39/uvloop/cbhandles.pyx#L268-L273).

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.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先阅读 uvloop/cbhandles.pyx 的第 268-273 行,并在 PYTHONASYNCIODEBUG=1 下使用 server.py 中的 FastAPI 复现,对比其 debug slow-log 行为与 asyncio 的实现。该 issue 没有定义可接受的输出或实现,因此在编码之前,请先明确所需的上下文和堆栈跟踪行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend, observability
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。