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
Add a test to check multiprocessing support
  • Loading branch information
seisman committed Jan 1, 2024
commit 48c00aee65d9a66ab94770ea6fb7bca6a1ece4d5
33 changes: 33 additions & 0 deletions pygmt/tests/test_session_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Test the session management modules.
"""
import os
from pathlib import Path

import pytest
from pygmt.clib import Session
Expand Down Expand Up @@ -57,3 +58,35 @@ def test_gmt_compat_6_is_applied(capsys):
# Make sure no global "gmt.conf" in the current directory
assert not os.path.exists("gmt.conf")
begin() # Restart the global session


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

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

import pygmt

reload(pygmt)
fig = pygmt.Figure()
fig.basemap(region=[10, 70, -3, 8], projection="X8c/6c", frame="afg")
fig.savefig(figname)


def test_session_multiprocessing():
"""
Make sure that the multiprocessing is supported if pygmt is re-imported.
"""

import multiprocessing as mp

fig_prefix = "test_session_multiprocessing"
with mp.Pool(2) as p:
p.map(gmt_func, [f"{fig_prefix}-1.png", f"{fig_prefix}-2.png"])
for i in [1, 2]:
assert Path(f"{fig_prefix}-{i}.png").exists()
Path(f"{fig_prefix}-{i}.png").unlink()
Loading