From eae345ed252c01e541d64c7e5b60b488d84268c6 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 8 Mar 2022 08:00:16 +0300 Subject: Create database connections within context managers Use the `with` context manager to open database connections, so as to ensure that those connections are closed once the call is completed. This hopefully avoids the 'too many connections' error --- scripts/partial_correlations.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'scripts') diff --git a/scripts/partial_correlations.py b/scripts/partial_correlations.py index ee442df..f203daa 100755 --- a/scripts/partial_correlations.py +++ b/scripts/partial_correlations.py @@ -35,19 +35,19 @@ def cleanup_string(the_str): return the_str.strip('"\t\n\r ') def run_partial_corrs(args): - try: - conn, _cursor_object = database_connector() - return partial_correlations_entry( - conn, cleanup_string(args.primary_trait), - tuple(cleanup_string(args.control_traits).split(",")), - cleanup_string(args.method), args.criteria, - cleanup_string(args.target_database)) - except Exception as exc: - print(traceback.format_exc(), file=sys.stderr) - return { - "status": "exception", - "message": traceback.format_exc() - } + with database_connector() as conn: + try: + return partial_correlations_entry( + conn, cleanup_string(args.primary_trait), + tuple(cleanup_string(args.control_traits).split(",")), + cleanup_string(args.method), args.criteria, + cleanup_string(args.target_database)) + except Exception as exc: + print(traceback.format_exc(), file=sys.stderr) + return { + "status": "exception", + "message": traceback.format_exc() + } def enter(): args = process_cli_arguments() -- cgit v1.2.3