python / python/cpython

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

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

@willingc đang làm issue này rồi.

Từ ngày 23/7/2026.

  • #143850 của @anordin95 — đang mở
docs topic-asyncio
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ả

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

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.

Đánh giá

Issue này chưa được đánh giá.

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.