Skip to content

Commit

Permalink
close AMCSD connection on exit of program / avoid crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Aug 10, 2023
1 parent 14fd14a commit 64b0a2f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions larch/xrd/amcsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from gzip import GzipFile
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

import atexit
import numpy as np

from sqlalchemy import MetaData, create_engine, func, text, and_, Table
Expand Down Expand Up @@ -665,7 +665,7 @@ def __init__(self, dbname=None, read_only=False):
raise ValueError("'%s' is not a valid AMCSD Database!" % dbname)

self.connect(dbname, read_only=read_only)

atexit.register(self.finalize_amcsd)
ciftab = self.tables['cif']
for colname in CIF_TEXTCOLUMNS:
if colname not in ciftab.columns and not read_only:
Expand All @@ -676,7 +676,11 @@ def __init__(self, dbname=None, read_only=False):
self.insert('version', tag=f'with {colname}', date=isotime(),
notes=f'added {colname} column to cif table')


def finalize_amcsd(self):
conn = getattr(self, 'conn', None)
if conn is not None:
conn.close()

def connect(self, dbname, read_only=False):
self.dbname = dbname
self.engine = make_engine(dbname)
Expand Down

0 comments on commit 64b0a2f

Please sign in to comment.