asyncio.as_completed() raises "no running event loop" when created outside a running loop (3.14 regression)
まだ誰も着手していません。
評価
調査の方向性
Lib/asyncio/tasks.py の _AsCompletedIterator.init から開始し、その後 asyncio.gather() における関連する処理と比較します。issue で言及されている回帰テストを確認し、関連する asyncio テストスイートを実行します。報告された loop.run_until_complete() パターンが 3.14 で動作し、既存の asyncio テストを壊さなければ完了です。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 558
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
docs pending
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
build type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
stdlib topic-email type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
zostera/django-bootstrap4#894 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
use-agent-os/agent-os#3276 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·