Skip to content

Commit 1b5a279

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

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pygeodiff/main.py

Lines changed: 10 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,12 @@ def __del__(self):
3337

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

3847
if self.context is None:
3948
self.context = self.clib.create_context()
@@ -43,8 +52,6 @@ def shutdown(self):
4352
self.clib.destroy_context(self.context)
4453
self.context = None
4554

46-
if self.clib is not None:
47-
self.clib.shutdown()
4855
self.clib = None
4956
self.callbackLogger = None
5057

0 commit comments

Comments
 (0)