Skip to content

Commit

Permalink
Set GMT_SESSION_NAME to a unique name on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Jan 1, 2024
1 parent 925e90e commit 7d09bcd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pygmt/session_management.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""
Modern mode session management modules.
"""
import os
import sys

from pygmt.clib import Session
from pygmt.helpers import unique_name


def begin():
Expand All @@ -12,6 +16,9 @@ def begin():
Only meant to be used once for creating the global session.
"""
if sys.platform == "win32":
os.environ["GMT_SESSION_NAME"] = unique_name()

Check warning on line 20 in pygmt/session_management.py

View check run for this annotation

Codecov / codecov/patch

pygmt/session_management.py#L20

Added line #L20 was not covered by tests

prefix = "pygmt-session"
with Session() as lib:
lib.call_module(module="begin", args=prefix)
Expand Down
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,8 @@
Test the session management modules.
"""
import os
import sys
from pathlib import Path

import pytest
from pygmt.clib import Session
Expand Down Expand Up @@ -57,3 +59,34 @@ 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 for multiprocessing support in PyGMT.
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 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()

0 comments on commit 7d09bcd

Please sign in to comment.