Skip to content

Commit

Permalink
Unique session in each process: version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Aug 13, 2024
1 parent bcb070c commit 3648074
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 0 additions & 1 deletion pygmt/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

_STATE = {
"session_name": None,
"module_calls": None,
}
16 changes: 9 additions & 7 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,20 @@ def __enter__(self):
Calls :meth:`pygmt.clib.Session.create`.
"""
_init_cli_session = False
# This is the first time a Session object is created.
if _STATE["session_name"] is None:
# Set GMT_SESSION_NAME to a customized, unique value.
_STATE["session_name"] = os.environ["GMT_SESSION_NAME"] = unique_name()
# Need to initialize the GMT CLI session.
_init_cli_session = True
self.create("pygmt-session")

if _init_cli_session:
self.call_module("begin", args=["pygmt-session"])
self.call_module(module="set", args=["GMT_COMPATIBILITY=6"])
del _init_cli_session

return self

def __exit__(self, exc_type, exc_value, traceback):
Expand Down Expand Up @@ -630,12 +639,6 @@ def call_module(self, module: str, args: str | list[str]):
GMTCLibError
If the returned status code of the function is non-zero.
"""
if _STATE["module_calls"] is None:
from pygmt.session_management import begin

_STATE["module_calls"] = []
begin()

c_call_module = self.get_libgmt_func(
"GMT_Call_Module",
argtypes=[ctp.c_void_p, ctp.c_char_p, ctp.c_int, ctp.c_void_p],
Expand Down Expand Up @@ -664,7 +667,6 @@ def call_module(self, module: str, args: str | list[str]):
"'args' must be either a string or a list of strings."
)

_STATE["module_calls"].append(module)
status = c_call_module(self.session_pointer, module.encode(), mode, argv)
if status != 0:
raise GMTCLibError(
Expand Down

0 comments on commit 3648074

Please sign in to comment.