【Azure 应用服务】Python fastapi Function在Azure中遇见AttributeError异常(AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async')
问题描述
参考文档“Using FastAPI Framework with Azure Functions”, 使用FastAPI 模块在Function中实现API请求。通过VS Code本地运行成功。
但是部署到Azure Function App后,遇见了如下错误:
[2023-01-30T09:23:15.474Z] Executing 'Functions.WrapperFunction' (Reason='This function was programmatically called via the host APIs.', Id=f49afe7c-b128-463e-0e93-c2f41f15bbde)
[2023-01-30T09:23:15.588Z] Executed 'Functions.WrapperFunction' (Failed, Id=f49afe7c-b128-463e-0e93-c2f41f15bbde, Duration=145ms)
[2023-01-30T09:23:15.588Z] System.Private.CoreLib: Exception while executing function: Functions.WrapperFunction. System.Private.CoreLib: Result: Failure
[2023-01-30T09:23:15.588Z] Exception: AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async'
关于AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async'错误,有什么办法来解决呢?
问题解决
根据在Github上寻找到相同错误的Issue分析,这个错误与Python的版本有关,在Python 3.9有遇见,但升级使用Python 3.10时,问题得到解决。
此外,Function App也提供了另外一种方式来解决该问题:设置应用配置 PYTHON_ISOLATE_WORKER_DEPENDENCIES 的值为 1 。
可以参考下图,在Azure Function App的门户中,设置 PYTHON_ISOLATE_WORKER_DEPENDENCIES
参考资料
AttributeError: 'AsgiMiddleware' object has no attribute 'handle_async' when running locally #7:https://github.com/Azure-Samples/fastapi-on-azure-functions/issues/7
[END]
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!