I'm studying Flask and tried to create small website on Heroku. I got timeout error with long task when deploy on Heroku and can by pass with timeout increasement. After investigate more, I found another solution is streamming. Here's article close with my solution: https://librenepal.com/article/flask-and-heroku-timeout/ But it's not work. Error still appears after 30 second Code from the article:
from flask import Flask, Response
import requests
app = Flask(__name__)
def some_long_calculation(number):
'''
here will be some long calculation using this number
let's simulate that using sleep for now :)
'''
import time
time.sleep(5)
return number
@app.route('/')
def check():
def generate():
for i in range(10):
yield "<br/>" # notice that we are yielding something as soon as possible
yield str(some_long_calculation(i))
return Response(generate(), mimetype='text/html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
Do you have any idea about this problem?
EDIT: I originally answered this question in 2016 when the below information was true. Today, you can indeed surpass the 30-second timeout using streaming responses.
You cannot get around the 30 second timeout behavior on the request-response cycle on Heroku. This is enforced through the Heroku routing mesh.
If you need to make long-running requests like this, you have a few options: