首页 > 解决方案 > 如何调用适用于 localhost 的 azure 函数并在 azure 门户上返回 500?

问题描述

我在 VSCode 中使用 Python 创建了一个 Azure 函数。API 包含对 AzureWebJob 的访问,当我在本地主机 http://localhost:7071/api/FUNCTIONNAME?Param1=value1 中对其进行测试时,响应为 200 并且 JSON 返回正常但是,当我将函数部署到Azure 链接https://APPSERVICENAME.azurewebsites.net/api/FUNCTIONNAME?code=APICODE==&Param1=value1返回 500 内部服务器错误。

我正在使用 Postman 在 Chrome 上测试并回复查询。

使用的代码是:

import logging
import azure.functions as func
from io import StringIO
import pandas as pd
from datetime import datetime as dt
import json

#filter the dataframe
def filter(pdf, keys, daterange):
  jkeys=[]
  for key in keys:
    #filter by numeric key
    if(reg.isdigit()):
      jkeys.append(int(key))
  #filtering by two types of keys
  pdfA=pdf[pdf['key'].isin(keys)]
  pdfS=pdf[pdf['numerickey'].isin(jkeys)]
  #concat the DFs
  pdf=pd.concat([pdfS,pdfA],axis=0)
  #Filtering by a dateRange or single date
  if(daterange.isdigit()): 
    pdf=pdf[pdf['Day']==int(daterange)]
    return pdf
  else:
    #getting the two dates
    date=daterange.split('-')
    #filtering the rows
    pdf=pdf[(pdf['Day']>=int(date[0])) & (pdf['Day']<=int(date[1]))]
    return pdf

# function to validate the params
def param_validation(param: str,
                    param_name: str,
                    req: func.HttpRequest) -> str:
                    
    if not param:
        try:
            req_body = req.get_json()
        except ValueError:

            if param != None and '-' in param:
                return func.HttpResponse(
                    "Set a date in the query",
                    status_code=400
                )
        else:
            param = req_body.get(param_name)
            return param
    else:
        return param

def main(req: func.HttpRequest,
            inputBlob: func.InputStream) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    #get the params
    daterange = req.params.get('daterange')
    keys = req.params.get('keys')
    name=req.params.get('name')

    daterange = param_validation(daterange, 'daterange', req)
    keys = param_validation(keys, 'keys', req)

    #this condition is included just to test fast if works 
    if name:
        return func.HttpResponse(f"Hello {name}!")
    #This is to get the real data request function
    if daterange and (keys):
        try:
            
            if daterange and keys:
                # get the data from a csv in the datalake 
                data = pd.read_csv(StringIO(inputBlob.read().decode('utf-8')))
                keys=keys.split(',')  
                dfFilt=filter(data,keys,daterange).fillna(0)

                return func.HttpResponse(f"{json.dumps(dfFilt.to_dict(orient='record'))}")

            else:
                return func.HttpResponse(
                    "insert a valid daterange to the query",
                    status_code=400
            )
        except:
                return func.HttpResponse(
                    "Params are wrong.",
                    status_code=500
            )
    else:
            return func.HttpResponse(
                "Params missed",
                status_code=400
            )
          

目的是从 datalake 获取 csv 并返回由提供的 2 个 args 过滤的行。

我找到了有关该错误的更多信息,平台返回137 python 代码。我使用陈旧的 csv(更轻,60mb)部署了最后一个功能性 api,并且 api 运行良好。但是当我设置新的 csv (300mb) 时,api 开始返回代码 500 内部服务器错误。

我认为这只是这个内存问题,因为在本地主机中完美运行。

是从 datalake 获取数据的另一种方法,使用 azure 函数过滤两列?或者它可以解决这个内存问题?

谢谢!

标签: pythonazure-functions

解决方案


此问题可能由多种原因引起,因此请显示更多详细信息。在我这边,没问题。

1,参数代码应该是你的功能的关键(如果它不是匿名的。)

2,请检查您的代码,也许您使用的东西不存在。(所以请显示您的代码和其他文件。)

您可以使用 application Insights 检查详细信息错误,也可以直接查看日志文件:

转到https://yourfunctionappname.scm.azurewebsites.net/DebugConsole,然后单击LogFiles\Application\Functions\Function\HttpTrigger1>,您将在那里找到日志文件,单击并检查错误。

有任何更新请告诉我。


推荐阅读