MagicStack / MagicStack/uvloop

Allow context for tasks that block the event loop

Ouverte
#656 2 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
Cython
Étoiles
11.9k
Forks
616
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par lire les lignes 268-273 de uvloop/cbhandles.pyx et comparez son comportement de journalisation lente en mode debug avec l’implémentation d’asyncio, en utilisant la reproduction FastAPI dans server.py sous PYTHONASYNCIODEBUG=1. L’issue ne définit ni sortie ni implémentation acceptée ; clarifiez donc le contexte souhaité et le comportement de la trace de la pile avant de coder.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
backend, observability
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.