python / python/cpython

Issues in asyncio documentation section "A homemade asyncio.sleep"

Offen
#143,727 6 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@willingc arbeitet bereits daran.

Seit 23.7.2026.

docs topic-asyncio
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Documentation

Link to section:
https://docs.python.org/3/howto/a-conceptual-overview-of-asyncio.html#a-homemade-asyncio-sleep

From my perspective, there are two issues in this section:

  1. Creating a task for the sleep call in main (await asyncio.create_task(async_sleep(3))) hides if the implementation actually cedes control to the event loop as desired or if it blocks it - an implementation using time.sleep(seconds) would also behave sane here. Therefore, I think it should be changed to await async_sleep(3).
  2. The implementation is said to mimick asyncio.sleep(), which can make readers think that this could actually be a usable replacement that behaves similarly. However, in one crucial aspect it behaves the opposite way: It causes 100% CPU usage on one core due to the busy waiting approach, which is opposed to how a sleep should behave. Two options I see:
  • Add something like: "Note: This implementation uses busy waiting and will consume 100% CPU on one core, unlike the real asyncio.sleep(). This implementation is purely meant for educational purposes to demonstrate futures and event loop interaction."
  • Add a synchronous time.sleep(0.001) or such right before await YieldToEventLoop() (in both example implementations) and explain (trade-off, sub-optimal workaround, will cause undesired CPU usage but significantly less than without).
Linked PRs
  • gh-143850

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.