Skip to content

Instantly share code, notes, and snippets.

@davidszotten
Created October 24, 2016 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidszotten/dcf3fdab739dba38701a06f13388d585 to your computer and use it in GitHub Desktop.
Save davidszotten/dcf3fdab739dba38701a06f13388d585 to your computer and use it in GitHub Desktop.
import psycopg2
original_connect = psycopg2.connect
def connect_with_retry(*args, **kwargs):
retries = 3
while True:
retries -= 1
try:
return original_connect(*args, **kwargs)
except psycopg2.OperationalError as exc:
# Unfortunately exc.pgcode is None for connect_timeout.
if exc.args != (u'timeout expired\n',):
raise
if retries <= 0:
raise
def patch_psycopg2():
psycopg2.connect = connect_with_retry
patch_psycopg2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment