Skip to content

Commit 12383e1

Browse files
committed
Add a test to make sure the workaround for multiprocessing support works
1 parent 925e90e commit 12383e1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pygmt/tests/test_session_management.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test the session management modules.
33
"""
44
import os
5+
from pathlib import Path
56

67
import pytest
78
from pygmt.clib import Session
@@ -57,3 +58,34 @@ def test_gmt_compat_6_is_applied(capsys):
5758
# Make sure no global "gmt.conf" in the current directory
5859
assert not os.path.exists("gmt.conf")
5960
begin() # Restart the global session
61+
62+
63+
def _gmt_func_wrapper(figname):
64+
"""
65+
A wrapper for running PyGMT scripts with multiprocessing.
66+
67+
Currently, we have to import pygmt and reload it in each process. Workaround from
68+
https://github.com/GenericMappingTools/pygmt/issues/217#issuecomment-754774875.
69+
"""
70+
from importlib import reload
71+
72+
import pygmt
73+
74+
reload(pygmt)
75+
fig = pygmt.Figure()
76+
fig.basemap(region=[10, 70, -3, 8], projection="X8c/6c", frame="afg")
77+
fig.savefig(figname)
78+
79+
80+
def test_session_multiprocessing():
81+
"""
82+
Make sure that multiprocessing is supported if pygmt is re-imported.
83+
"""
84+
85+
import multiprocessing as mp
86+
87+
prefix = "test_session_multiprocessing"
88+
with mp.Pool(2) as p:
89+
p.map(_gmt_func_wrapper, [f"{prefix}-1.png", f"{prefix}-2.png"])
90+
Path(f"{prefix}-1.png").unlink()
91+
Path(f"{prefix}-2.png").unlink()

0 commit comments

Comments
 (0)