0

I made a python web app and deployed it to heroku successfully and it works well to an extent. The problem starts when once in a while the worker process throws a connection reset by peer error once in a while for which i have to go in and redeploy again only for it to happen again. This process affects the entire web app as those small glitches cause the entire program to malfunction and produce inconsistent if not wrong information, so I'm trying to validate if an exception handling statement in the following format would work:

def conti():
    opens the connection to the site
    performs the operations needed
    closes the connection

try:
    conti()
except:
    conti()

How can i make the try statement sort of recursive so that whenever the error happens it would still continue. Do i need to put the try statement in a recursive function to handle the error. Thank you.

  • I'm afraid your question is unclear / a bit too broad. As a general rule, the solution would be to use a loop (instead of recursion) to retry the failed operation, but how to do so properly in your case depends a lot of the exact context, and you really didn't provide much here. – bruno desthuilliers Jan 07 '19 at 09:39

1 Answers1

0

My recommendation is to consider a connection pool. If you are on Heroku and using PostgreSQL, you are probably already using psycopg2, which has a pool built in. See psycopg2 and infinite python script

This will avoid either recursion or explicit connection state/error detection in your code.

bimsapi
  • 4,985
  • 2
  • 19
  • 27