python runtime_error ai_generated true

RuntimeWarning: 协程 'fetch' 从未被 await

RuntimeWarning: coroutine 'fetch' was never awaited

ID: python/asyncio-coroutine-never-awaited

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-04-08首次发现

版本兼容性

版本状态引入弃用备注
3.8+ active
3.9+ active
3.10+ active
3.11+ active
3.12+ active

根因分析

调用了协程函数但返回的协程对象既未 await 也未调度,导致函数体从未执行。

English

A coroutine function was called but the returned coroutine object was not awaited or scheduled, so the body never executed.

generic

解决方案

  1. 97% 成功率
    await fetch(url)
    # or
    asyncio.create_task(fetch(url))
  2. 85% 成功率
    PYTHONASYNCIODEBUG=1 python app.py
    # or in code:
    loop = asyncio.get_running_loop()
    loop.set_debug(True)

无效尝试

常见但无效的做法:

  1. 90% 失败

    Hides the warning but the coroutine still never runs; business logic silently missing.

  2. 85% 失败

    Both calls produce unawaited coroutine objects; neither executes.