diff --git a/thorlabs_apt/core.py b/thorlabs_apt/core.py index bf6c629..9d1332d 100644 --- a/thorlabs_apt/core.py +++ b/thorlabs_apt/core.py @@ -1343,10 +1343,16 @@ def set_dc_settled_current_loop_parameters(self, settled_proportional, """DC settled current loop: fast forward""" -def _load_library(): +_lib = None + +def load_library(): """ Loads the APT.dll shared library. Calls APTInit. """ + global _lib + if _lib is not None: + # The library has already been loaded + return # load library if (os.name != 'nt'): raise Exception("Your operating system is not supported. " \ @@ -1370,17 +1376,20 @@ def _load_library(): "%s" % _get_error_text(err_code)) if (lib.EnableEventDlg(False) != 0): raise Exception("Couldn't disable event dialog.") + # Store the library globally + _lib = lib return lib -_lib = None -_lib = _load_library() +load_library() import atexit @atexit.register -def _cleanup(): +def cleanup(): """ - Calls APTCleanUp + Calls APTCleanUp. """ + global _lib if (_lib is not None): _lib.APTCleanUp() + _lib = None