Wednesday, 15 May 2019

how to close db connections while making flask API calls in a cron job?

The cronjob connects to different databases using different API calls using an admin session. After each call I would like to ensure that the connection is closed. In case of normal API call I can use before_request to open and after_request decorator to close after each API call, but in cronjob how to call these functions. The code in the cron job to list queues for one of the databases is as follows:

def get_queue_list(session, access_token):
    queue_list = []
    headers={'Authorization': "Bearer " + access_token}
    r = session.get(queue_api, headers=headers)
    queue_list = json.loads(r.content)['queue_list']
    r.connection.close()

    return queue_list

In the above code snippet, does the statement r.connection.close() ensure that the db connection is closed? How do I check it? I have many installs on the same server. This cronjob calls different APIs for the different installs of different databases to process periodically. Please quide

The main function call:

if __name__ == "__main__":
    print __doc__
    # base_url is read from a csv file
    queue_api = base_url + '/rt/api/v1.0/queues'



from how to close db connections while making flask API calls in a cron job?

No comments:

Post a Comment