Skip to content

Commit 7d09bcd

Browse files
committed
Set GMT_SESSION_NAME to a unique name on Windows
1 parent 925e90e commit 7d09bcd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pygmt/session_management.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""
22
Modern mode session management modules.
33
"""
4+
import os
5+
import sys
6+
47
from pygmt.clib import Session
8+
from pygmt.helpers import unique_name
59

610

711
def begin():
@@ -12,6 +16,9 @@ def begin():
1216
1317
Only meant to be used once for creating the global session.
1418
"""
19+
if sys.platform == "win32":
20+
os.environ["GMT_SESSION_NAME"] = unique_name()
21+
1522
prefix = "pygmt-session"
1623
with Session() as lib:
1724
lib.call_module(module="begin", args=prefix)

pygmt/tests/test_session_management.py

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

68
import pytest
79
from pygmt.clib import Session
@@ -57,3 +59,34 @@ def test_gmt_compat_6_is_applied(capsys):
5759
# Make sure no global "gmt.conf" in the current directory
5860
assert not os.path.exists("gmt.conf")
5961
begin() # Restart the global session
62+
63+
64+
def gmt_func(figname):
65+
"""
66+
Workaround for multiprocessing support in PyGMT.
67+
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+
fig_prefix = "test_session_multiprocessing"
88+
with mp.Pool(2) as p:
89+
p.map(gmt_func, [f"{fig_prefix}-1.png", f"{fig_prefix}-2.png"])
90+
for i in [1, 2]:
91+
assert Path(f"{fig_prefix}-{i}.png").exists()
92+
Path(f"{fig_prefix}-{i}.png").unlink()

0 commit comments

Comments
 (0)