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
相关产品推荐
相关产品推荐

