I'm writing a python app that instructs a database to perform various processing instructions on partitioned tables. The processing can take long enough that there might be a timeout, so I surround my calls to the database with try:... except:
blocks like:
while True:
try:
process_one(table, logger, cursor)
except OperationalError as oe:
logger.error(oe)
con, cursor = retry_connection(logger, dbset)
else:
break
Where process_one()
is a call to a PostgreSQL function like:
def process_one(table, logger, cursor):
logger.info('Processing table %s', table)
cursor.execute('SELECT do_something(%(table)s)', {'table':table})
But if I have a sequence of processing functions to execute, and don't want to repeat a procedure in case of a connection issue this would turn into:
while True:
try:
process_one(...)
....
while True:
try:
process_two(...)
....
Would the DRY ideal be to have a function that performs the while True, try, except, break
loop and takes different processing functions as a parameter?
def try_process(process, processparams):
while True:
try:
process(processparams)
except OperationalError as oe:
...