asyncio.as_completed() raises "no running event loop" when created outside a running loop (3.14 regression)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
Since 3.14, asyncio.as_completed() raises RuntimeError: no running event loop when the iterator is created outside of a running event loop, even though the futures are then driven with loop.run_until_complete(). This pattern works on 3.13 and is the same pattern that asyncio.gather() still supports.
import asyncio
async def work(i):
await asyncio.sleep(0.01 * i)
return i
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
tasks = [loop.create_task(work(i)) for i in (2, 1)]
for f in asyncio.as_completed(tasks):
print(loop.run_until_complete(f))
loop.close()
3.13:
1
2
main (and 3.14):
Traceback (most recent call last):
File "repro.py", line 10, in <module>
for f in asyncio.as_completed(tasks):
~~~~~~~~~~~~~~~~~~~~^^^^^^^
File ".../Lib/asyncio/tasks.py", line 581, in __init__
self._cur_task = current_task()
RuntimeError: no running event loop
The cause is the awaited-by tracking added in gh-91048: _AsCompletedIterator.__init__ calls current_task() unconditionally. gather() got the same tracking but guards it with events._get_running_loop() and falls back to cur_task = None (which future_add_to_awaited_by() already treats as a no-op). as_completed() should do the same.
I have a fix with a regression test ready.
CPython versions tested on:
CPython main branch, 3.14
Operating systems tested on:
macOS
Linked PRs
- gh-157857
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong Lib/asyncio/tasks.py tại _AsCompletedIterator.init, sau đó so sánh với cách xử lý liên quan trong asyncio.gather(). Xem lại bài kiểm thử hồi quy được đề cập trong issue và chạy bộ kiểm thử asyncio liên quan. Công việc được xem là hoàn tất khi mẫu loop.run_until_complete() được báo cáo hoạt động trên 3.14 mà không làm hỏng các bài kiểm thử asyncio hiện có.
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
- Loại issue
- Lỗi
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100