pythonazureflaskgatewayazure-webapps

How to prevent the 230 seconds azure gateway timeout using python flask for long running work loads


I have a python flask application as a azure web app and one function is a compute intensive workload which takes more than 5 minutes to process, is there any hack to prevent the gateway time out error by keeping the TCP connection active between the client and the api while the function is processing the data? Sample of current code below.

from flask import Flask

app = Flask(__name__)

@app.route('/data')
def data():
    mydata = super_long_process_function()
    # takes more than 5 minutes to process
    return mydata 

Since the super_long_process_function takes more than 5 minutes, it always times out with 504 Gateway Time-out. One thing I want to mention is that this is idle timeout at the TCP level which means that if the connection is idle only and no data transfer happening, only then this timeout is hit. So is there any hack in flask that can be used to prevent this timeout while we process the data because based on my research and reading Microsoft documentation the 230 seconds limit cannot be changed for web apps.


Solution

  • In short: the 230 second timeout, as you stated, cannot be changed.

    230 seconds is the maximum amount of time that a request can take without sending any data back to the response. It is not configurable.

    Source: GitHub issue

    The timeout occurs when there's no response. Keeping the connection open and sending data will not help.

    There are a couple of ways you can go about this. Here are two of more possible solutions you could use to trigger your long running tasks without the timeout being an issue.

    1. Only trigger the long running task with an HTTP call, but don't wait for their completion before returning a response.
    2. Trigger the task using a messaging mechanism like Storage Queues or Service Bus.

    For updating the web application with the result of the long running task, think along the lines of having the response hold a URL the frontend can call to check for task completion periodically, your request having a callback URL to call when the task has completed or implementing Azure Web PubSub for sending status updates to the client.