From 77700a8dd0b6671e1024cab46147f10a92cf8bf0 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 14 Aug 2024 00:27:50 +0800 Subject: [PATCH 1/2] Support multiprocessing --- pygmt/__init__.py | 3 +++ pygmt/session_management.py | 8 -------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/pygmt/__init__.py b/pygmt/__init__.py index dbf292e4f1f..91763ebdc97 100644 --- a/pygmt/__init__.py +++ b/pygmt/__init__.py @@ -19,6 +19,7 @@ """ import atexit as _atexit +import os # Import modules to make the high-level GMT Python API from pygmt import datasets @@ -66,6 +67,8 @@ xyz2grd, ) +# Set the session name to the current process ID. Necessary for multiprocessing support. +os.environ["GMT_SESSION_NAME"] = str(os.getpid()) # Start our global modern mode session _begin() # Tell Python to run _end when shutting down diff --git a/pygmt/session_management.py b/pygmt/session_management.py index 87055bb44e8..b8093859402 100644 --- a/pygmt/session_management.py +++ b/pygmt/session_management.py @@ -2,11 +2,7 @@ Modern mode session management modules. """ -import os -import sys - from pygmt.clib import Session -from pygmt.helpers import unique_name def begin(): @@ -17,10 +13,6 @@ def begin(): Only meant to be used once for creating the global session. """ - # On Windows, need to set GMT_SESSION_NAME to a unique value - if sys.platform == "win32": - os.environ["GMT_SESSION_NAME"] = unique_name() - prefix = "pygmt-session" with Session() as lib: lib.call_module(module="begin", args=[prefix]) From 89966b225f722d006917cdd28b3d230fccb8ff9f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 14 Aug 2024 00:39:38 +0800 Subject: [PATCH 2/2] Remove the workaround from the test_session_multiprocessing test --- pygmt/tests/test_session_management.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pygmt/tests/test_session_management.py b/pygmt/tests/test_session_management.py index d949f1a51c0..21cd095e5b2 100644 --- a/pygmt/tests/test_session_management.py +++ b/pygmt/tests/test_session_management.py @@ -3,9 +3,9 @@ """ import multiprocessing as mp -from importlib import reload from pathlib import Path +import pygmt import pytest from pygmt.clib import Session from pygmt.session_management import begin, end @@ -65,13 +65,7 @@ def test_gmt_compat_6_is_applied(capsys): def _gmt_func_wrapper(figname): """ A wrapper for running PyGMT scripts with multiprocessing. - - Currently, we have to import pygmt and reload it in each process. Workaround from - https://github.com/GenericMappingTools/pygmt/issues/217#issuecomment-754774875. """ - import pygmt - - reload(pygmt) fig = pygmt.Figure() fig.basemap(region=[10, 70, -3, 8], projection="X8c/6c", frame="afg") fig.savefig(figname)