[Python] AZure function api Times out at the second call while working locally

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am using an azure function app to get some weather data using open-meteo api, to do so I need to call their API from inside my function APP. Before deploying on azure, the http triggered function works without any issue and responses are in a matter of seconds. when I deploy It to an azure app it works the first time then times out the second time then works again from different laptops but times out after.

    I have tried multiple approaches: -Used a context manager to ensure the session is closed after use. -Implemented retries with exponential backoff using the retry_requests library -creased the timeout and memory allocation for your Azure Function.

    But nothing is working, below is the part of my code that gets the data from the api:

    def fetch_and_process_data(lat, lon, input_str):
    today_str, formatted_datetime = format_datetime(input_str)
    retry_session = retry(requests.Session(), retries=5, backoff_factor=0.5)
    openmeteo = openmeteo_requests.Client(session=retry_session)
    # Fetch weather data
    weather_url = "https://api.open-meteo.com/v1/forecast"
    weather_params = {
    "latitude": lat,
    "longitude": lon,
    "timezone": "Europe/London",
    "hourly": ["wind_speed_10m", "wind_direction_10m"],
    "wind_speed_unit": "ms",
    "start_date": today_str,
    "end_date": today_str
    }
    weather_responses = openmeteo.weather_api(weather_url, params=weather_params)
    weather_response = weather_responses[0]
    hourly_weather = weather_response.Hourly()
    weather_data = {
    "date": pd.date_range(
    start=pd.to_datetime(hourly_weather.Time(), unit="s", utc=True),
    end=pd.to_datetime(hourly_weather.TimeEnd(), unit="s", utc=True),
    freq=pd.Timedelta(seconds=hourly_weather.Interval()),
    inclusive="left"
    ),
    "wind_speed_10m": hourly_weather.Variables(0).ValuesAsNumpy(),
    "wind_direction_10m": hourly_weather.Variables(1).ValuesAsNumpy()
    }
    weather_dataframe = pd.DataFrame(data=weather_data)


    When used locally it works as expected but not when deployed to azure.

    What might be the issue?

    any help would be much appreciated

    Continue reading...

Compartilhe esta Página