Skip to content

Commit a2142ff

Browse files
committed
Use global session
1 parent dbb5c33 commit a2142ff

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

pygmt/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from pygmt.accessors import GMTDataArrayAccessor
2727
from pygmt.figure import Figure, set_display
2828
from pygmt.io import load_dataarray
29-
from pygmt.session_management import begin as _begin
3029
from pygmt.session_management import end as _end
3130
from pygmt.src import (
3231
binstats,
@@ -66,7 +65,5 @@
6665
xyz2grd,
6766
)
6867

69-
# Start our global modern mode session
70-
_begin()
7168
# Tell Python to run _end when shutting down
7269
_atexit.register(_end)

pygmt/_state.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Private dictionary to keep tracking of current PyGMT state.
3+
4+
The feature is only meant for internal use by PyGMT and is experimental!
5+
"""
6+
7+
_STATE = {
8+
"session_name": None,
9+
"module_calls": None,
10+
}

pygmt/clib/session.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import contextlib
99
import ctypes as ctp
10+
import os
1011
import pathlib
1112
import sys
1213
import warnings
@@ -17,6 +18,7 @@
1718
import pandas as pd
1819
import xarray as xr
1920
from packaging.version import Version
21+
from pygmt._state import _STATE
2022
from pygmt.clib.conversion import (
2123
array_to_datetime,
2224
as_c_contiguous,
@@ -33,6 +35,7 @@
3335
data_kind,
3436
tempfile_from_geojson,
3537
tempfile_from_image,
38+
unique_name,
3639
)
3740

3841
FAMILIES = [
@@ -209,6 +212,10 @@ def __enter__(self):
209212
210213
Calls :meth:`pygmt.clib.Session.create`.
211214
"""
215+
# This is the first time a Session object is created.
216+
if _STATE["session_name"] is None:
217+
# Set GMT_SESSION_NAME to a customized, unique value.
218+
_STATE["session_name"] = os.environ["GMT_SESSION_NAME"] = unique_name()
212219
self.create("pygmt-session")
213220
return self
214221

@@ -623,6 +630,12 @@ def call_module(self, module: str, args: str | list[str]):
623630
GMTCLibError
624631
If the returned status code of the function is non-zero.
625632
"""
633+
if _STATE["module_calls"] is None:
634+
from pygmt.session_management import begin
635+
636+
_STATE["module_calls"] = []
637+
begin()
638+
626639
c_call_module = self.get_libgmt_func(
627640
"GMT_Call_Module",
628641
argtypes=[ctp.c_void_p, ctp.c_char_p, ctp.c_int, ctp.c_void_p],
@@ -651,6 +664,7 @@ def call_module(self, module: str, args: str | list[str]):
651664
"'args' must be either a string or a list of strings."
652665
)
653666

667+
_STATE["module_calls"].append(module)
654668
status = c_call_module(self.session_pointer, module.encode(), mode, argv)
655669
if status != 0:
656670
raise GMTCLibError(

pygmt/session_management.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
Modern mode session management modules.
33
"""
44

5-
import os
6-
import sys
7-
5+
from pygmt._state import _STATE
86
from pygmt.clib import Session
9-
from pygmt.helpers import unique_name
107

118

129
def begin():
@@ -17,10 +14,6 @@ def begin():
1714
1815
Only meant to be used once for creating the global session.
1916
"""
20-
# On Windows, need to set GMT_SESSION_NAME to a unique value
21-
if sys.platform == "win32":
22-
os.environ["GMT_SESSION_NAME"] = unique_name()
23-
2417
prefix = "pygmt-session"
2518
with Session() as lib:
2619
lib.call_module(module="begin", args=[prefix])
@@ -37,5 +30,11 @@ def end():
3730
background, convert them to the desired format (specified in
3831
``pygmt.begin``), and bring the figures to the working directory.
3932
"""
33+
if _STATE["session_name"] is not None:
34+
return
35+
4036
with Session() as lib:
4137
lib.call_module(module="end", args=[])
38+
39+
# Reset the sesion name to None
40+
_STATE["session_name"] = None

0 commit comments

Comments
 (0)