I’ve got a Python Flask application deployed as a Google Cloud Run service, which listens on port 8080 for incoming requests. Most requests I make to this endpoint return the expected output, however, when I pass specific URL parameters that make the function run for much longer (from around 5-10 minutes to 20 minutes), the application does not return a response to the client. The client is a simple requests.post()
function:
response = requests.post(
f'{service_url}/run',
headers={
'Authorization': f"Bearer {self.bearer_token}",
"Content-Type": "application/json",
'x-environment': 'production'
}
)
response.raise_for_status()
return response
I have confirmed that the function itself runs successfully, and also that the print('Finished!)
line runs from the logs. There are no errors returned. I have also confirmed that the timeout in Cloud Run is set to more than enough – 3600 seconds/1 hour
I’ve tried running the application locally and cannot reproduce, so it’s something to do with Cloud Run.
Anyone got any ideas? I’m at a total loss. The task that makes this request is part of a bigger job that has downstream dependencies, so running asynchronously (eg. using threading to return a response to the client whilst the operation runs in the background) is something I would rather avoid.
@app.route('/run', methods=['POST'])
def run():
// My long-running single threaded function is here
if is_error:
status_code = 500
else:
status_code = 200
response = make_response(result, status_code)
response.close()
print('Finished!)
return response
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)