python runtime_error ai_generated true

RuntimeError: 协程对象不可调用

RuntimeError: coroutine object is not callable

ID: python/fastapi-runtimeerror-coroutine-object-is-not-callable

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

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

尝试将协程作为函数调用而未使用 await,或者将协程赋值给变量后调用。

English

Trying to call a coroutine as a function without using await, or assigning a coroutine to a variable and then calling it.

generic

解决方案

  1. 95% 成功率 Use await when calling async functions.
    async def get_data():
        return await fetch_data()
  2. 85% 成功率 Run the coroutine in an event loop if outside async context.
    import asyncio
    result = asyncio.run(async_function())

无效尝试

常见但无效的做法:

  1. Calling an async function without await inside a sync function. 80% 失败

    Sync functions cannot use await; calling an async function returns a coroutine object, not the result.

  2. Storing a coroutine in a variable and then using () on it. 70% 失败

    The variable holds a coroutine object; calling it again tries to call the coroutine, which is not allowed.