Python on heroku gives h12

Viewed 76

I coded an api Nodejs with python fecthing data and making some plots to pdf file. It' just simple api works with 100-200 datarow in pandas.

So It works fine in my local machine but when I push it to heroku(free tier) it gives h12 timeout error.How can I improve my api speed, is there a way to python build or npm built to make production version of the api. Thank you.

1 Answers

It's one of the hardest things in Heroku. Usually, Heroku will give you time out after the 30s. But there is a lot of work around according to your type of app.

1. Flask and streaming response

Heroku will give a timeout error if your app returns nothing in the 30s. So you can stream your response while processing is the one way to overcome this error. For more details look here and here.

2. Run your long process in the background

Another way to bypass this time out error is, you can send the longest time taking process to the background task list there is no time limit in the background task. You can see here more about it

3. Change timeout in gunicorn

If you are using gunicorn and Flask you can bypass the error with the trick refer the gunicorn document here. Still, you cannot set the timeout larger than the 30s but you can terminate the process less than the 30s without error and you can re-run

I think in your case streaming response will do the job. Just iterate through each row and stream response after every row processed.

Related