Skip to content

Commit 52bc5e1

Browse files
committed
Keep only one GeoDiffLib instance per library
1 parent 9461de8 commit 52bc5e1

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

pygeodiff/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
:license: MIT, see LICENSE for more details.
88
"""
99

10+
import weakref
1011
from .geodifflib import GeoDiffLib
1112

1213

@@ -15,6 +16,9 @@ class GeoDiff:
1516
geodiff is a module to create and apply changesets to GIS files (geopackage)
1617
"""
1718

19+
# Dictionary of libname to instance of GeoDiffLib
20+
_clib_cache = weakref.WeakValueDictionary()
21+
1822
def __init__(self, libname=None):
1923
"""
2024
if libname is None, it tries to import c-extension from wheel
@@ -33,7 +37,11 @@ def __del__(self):
3337

3438
def _lazy_load(self):
3539
if self.clib is None:
36-
self.clib = GeoDiffLib(self.libname)
40+
if clib := GeoDiff._clib_cache.get(self.libname):
41+
self.clib = clib
42+
else:
43+
self.clib = GeoDiffLib(self.libname)
44+
GeoDiff._clib_cache[self.libname] = self.clib
3745

3846
if self.context is None:
3947
self.context = self.clib.create_context()
@@ -43,8 +51,6 @@ def shutdown(self):
4351
self.clib.destroy_context(self.context)
4452
self.context = None
4553

46-
if self.clib is not None:
47-
self.clib.shutdown()
4854
self.clib = None
4955
self.callbackLogger = None
5056

0 commit comments

Comments
 (0)