[Python] How to call a azure function that works on localhost and return 500 on azure portal?

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Azure Function created in VSCode with Python. The API contain the access to a AzureWebJob, when I tested it in the local host http://localhost:7071/api/FUNCTIONNAME?Param1=value1 the response is 200 and the JSON return is ok but, when I deploy the function to Azure the link https://APPSERVICENAME.azurewebsites.net/api/FUNCTIONNAME?code=APICODE==&Param1=value1 return 500 Internal Server Error.

    I'm using Postman to test and replied the query on Chrome.

    The code used is:

    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
    )



    The purpose is to get a csv from datalake and return the rows filtered by 2 args provided.

    I found more information about the error, the platform return a 137 python code. I deployed the last functional api with a stale csv (that is lighter, 60mb) and the api works good. But when I a set the new csv (300mb) the api starts to return the code 500 internal server error.

    I think that is just this memory problem, because in the local host works perfectly.

    are another way to get the data from datalake, filtering two columns using a azure function? Or its posible to fix this memory problem ?

    Thanks!

    Continue reading...

Compartilhe esta Página