工作人员无法使用 AsgiMiddleware 为 Azure Functions 和 FastAPI 本地索引函数

问题描述 投票:0回答:1

遇到重启后出现的错误。我正在结合使用 Azure Functions 和 FastAPI 进行测试,基于:https://dev.to/manukanne/azure-functions-and-fastapi-14b6

代码正在运行,测试 API 调用按预期工作。重新启动计算机并重新启动 VSCode 后,我在尝试本地运行时遇到了问题。

我完成了SO:76842742,设置略有不同,但看到了类似类型的错误。

虚拟环境正在按预期运行,requirements.txt 显示已安装。也已使用详细标志运行,但未提供其他错误消息。

当前错误:
Worker 未能索引函数 结果:失败异常:ValueError:无法在 function_app.py 中找到顶级函数应用实例。

我的 function.json 有 scriptFile: init.py 我认为它需要是 function_app.py 但错误表明它正在寻找 function_app.py 无论如何。我创建了 function_app.py 的副本并将其命名为“__init __.py”作为潜在的修复方案,但产生了相同的错误代码。

此时我不知道为什么没有找到索引函数。关于潜在的解决方案有什么想法吗?

找到Python版本3.11.0(py)
核心工具版本:4.0.5611
VS 代码:1.88.1

function_app.py

import azure.functions as func
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import logging

app = FastAPI()

@app.exception_handler(Exception)
async def handle_exception(request: Request, exc: Exception):
    return JSONResponse(
        status_code=400,
        content={"message": str(exc)},
    )

@app.get("/")
async def home():
    return {
        "info": "Try the API path for success"
    }

@app.get("/v1/test/{test}")
async def get_test(
    test: str,):
    return {
        "test": test,
    }

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.AsgiMiddleware(app).handle_async(req, context)

函数.json

{
    "scriptFile": "__init__.py",
    "bindings": [
      {
        "authLevel": "function",
        "type": "httpTrigger",
        "direction": "in",
        "name": "req",
        "methods": [
          "get",
          "post"
        ],
        "route": "/{*route}"
      },
      {
        "type": "http",
        "direction": "out",
        "name": "$return"
      }
    ]
  }

额外添加
主机.json

{
  "$schema": "https://raw.githubusercontent.com/Azure/azure-functions-host/dev/schemas/json/host.json",
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  },
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  }
}
python azure-functions fastapi
1个回答
0
投票

根据 JarroVGIT 的评论,我将 function_app.py 代码重构为:

import azure.functions as func
from fastapi import FastAPI, Request, Response
import logging

fast_app = FastAPI()

@fast_app.exception_handler(Exception)
async def handle_exception(request: Request, exc: Exception):
    return Response(
        status_code=400,
        content={"message": str(exc)},
    )

@fast_app.get("/")
async def home():
    return {
        "info": "Try the API path for success" 
    }

@fast_app.get("/v1/test/{test}")
async def get_test(
    test: str,):
    return {
        "test": test,
    }

app = func.AsgiFunctionApp(app=fast_app,
                           http_auth_level=func.AuthLevel.ANONYMOUS,)

删除 def main 并添加应用程序调用。这已经解决了问题并允许azure功能正常启动。

© www.soinside.com 2019 - 2024. All rights reserved.