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

POC + WIP: Support multiprocessing Version 2 #3399

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""

import atexit as _atexit
import os

# Import modules to make the high-level GMT Python API
from pygmt import datasets
Expand Down Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions pygmt/session_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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])
Expand Down
8 changes: 1 addition & 7 deletions pygmt/tests/test_session_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading