Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by avoiding loading the GMT library repeatedly #2930

Merged
merged 12 commits into from
Jan 2, 2024
Prev Previous commit
Next Next commit
Improve tests
  • Loading branch information
seisman committed Jan 1, 2024
commit 820e11b228cabb360ed2bdd1b5b175c85ccdf92a
13 changes: 6 additions & 7 deletions pygmt/tests/test_clib_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class TestLibgmtCount:
"""

loaded_libgmt = load_libgmt() # Load the GMT library and reuse it when necessary
counter = 0 # Count how many times ctypes.CDLL is called
counter = 0 # Global counter for how many times ctypes.CDLL is called

def _mock_ctypes_cdll_return(self, libname): # noqa: ARG002
"""
Expand All @@ -226,15 +226,14 @@ def _mock_ctypes_cdll_return(self, libname): # noqa: ARG002
self.counter += 1 # Increase the counter
return self.loaded_libgmt

@pytest.fixture()
def _mock_ctypes(self, monkeypatch):
monkeypatch.setattr(ctypes, "CDLL", self._mock_ctypes_cdll_return)

@pytest.mark.usefixtures("_mock_ctypes")
def test_libgmt_load_counter(self):
def test_libgmt_load_counter(self, monkeypatch):
"""
Make sure that the GMT library is not loaded in every session.
"""
# Monkeypatch the ctypes.CDLL function
monkeypatch.setattr(ctypes, "CDLL", self._mock_ctypes_cdll_return)

# Create two sessions and check the global counter
with Session() as lib:
_ = lib
with Session() as lib:
Expand Down
5 changes: 2 additions & 3 deletions pygmt/tests/test_session_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ def test_gmt_compat_6_is_applied(capsys):

def gmt_func(figname):
"""
Workaround to let PyGMT support multiprocessing.
Workaround for multiprocessing support in PyGMT.

See
https://github.com/GenericMappingTools/pygmt/issues/217#issuecomment-754774875
https://github.com/GenericMappingTools/pygmt/issues/217#issuecomment-754774875.
"""
from importlib import reload

Expand Down