You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python版Firebase Cloud Function异步函数(async/await)的正确用法及最佳实践咨询

Python版Firebase Cloud Function异步函数(async/await)的正确用法及最佳实践咨询

我最近在尝试给Python版的Firebase Cloud Function标记为async异步函数,但遇到了一些问题,想请教下正确的用法和相关最佳实践。

比如我写了这样的代码:

async def async_function() -> str:
    print("async function starting")
    await asyncio.sleep(1)
    print ("async function finished")
    return "finished"

@https_fn.on_request()
async def sample_cloud_function(req: https_fn.Request) -> https_fn.Response:
    result = await async_function()
    return https_fn.Response(result)

在本地使用模拟器运行时,会抛出类型错误:

TypeError: The view function did not return a valid response. The
return type must be a string, dict, list, tuple with headers or
status, Response instance, or WSGI callable, but it was a coroutine.

不过我能成功部署这个函数,但部署后调用时没有任何反应,还会收到一个警告说函数从未被await。而且我在官方文档里也没找到相关的说明,想问问这种场景下的最佳实践是什么?

备注:内容来源于stack exchange,提问作者yambo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 19:38:06