Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
123294c
grass.script: Make worker processes work with any multiprocessing sta…
claude Nov 27, 2026
b104040
grass.pygrass: Do not fix the default multiprocessing start method
claude Nov 27, 2026
ad49bf3
grass.script: Add type annotations to the worker process helpers
claude Nov 28, 2026
9875fa5
grass.pygrass: Fix the MultiModule doctests for any start method
claude Nov 28, 2026
d0e9b34
grass.script: Document the main guard and use one multiprocessing con…
claude Nov 28, 2026
019c15c
grass.temporal: Correct what happens without the main guard in the docs
claude Nov 28, 2026
e4c1021
grass.temporal: Check that the message server prints in the start met…
claude Nov 28, 2026
f658ed8
grass.script: Use the Path argument directly in the worker process test
claude Nov 28, 2026
f7fb2a0
Merge branch 'main' into claude/laughing-lovelace-44g9uv
echoix Nov 28, 2026
7025ddf
Merge main into worker start method branch
claude Dec 1, 2026
94ad70c
grass.temporal: Treat any nonzero exit code of the parallel tools as …
claude Dec 1, 2026
0716db4
t.rast.mapcalc: Test that any nonzero exit code of the parallel tools…
claude Dec 1, 2026
9639ef6
t.rast.extract: Test that any nonzero exit code of the parallel tools…
claude Dec 1, 2026
cd33c98
temporal: Check the error message in the exit code tests
claude Dec 1, 2026
6ca5592
Merge branch 'main' into claude/laughing-lovelace-44g9uv
echoix Dec 1, 2026
1756438
grass.script: Make test modules importable in spawn and forkserver wo…
claude Dec 2, 2026
c3d1168
Merge branch 'main' into claude/laughing-lovelace-44g9uv
claude Dec 3, 2026
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
211 changes: 121 additions & 90 deletions python/grass/docs/src/temporal_framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ Here the full list of all temporal modules:
Examples
--------

Scripts using the temporal framework or the *pygrass* servers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The temporal framework starts server processes (see ``tgis.init()``). They are
started with the default start method of the Python *multiprocessing* module:
*spawn* on Windows and macOS and, since Python 3.14, *forkserver* on Linux.
These methods import the main script again in the server process, so the code
which uses the temporal framework (or *pygrass* classes such as ``Messenger``
and ``DataProvider``) needs to be in a function called under
``if __name__ == "__main__":``, as in the examples below. Without it, the
server processes fail, so a script fails with an error such as
``Connection reset by peer`` or its messages are not printed.

Howto start example
^^^^^^^^^^^^^^^^^^^

Expand All @@ -275,48 +288,54 @@ to access its registered maps.
import grass.temporal as tgis
import grass.script as gs

# Make sure the temporal database exists
# and set the temporal GIS environment
tgis.init()

# We create the temporal database interface for fast processing
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()
def main():
# Make sure the temporal database exists
# and set the temporal GIS environment
tgis.init()

# The id of a space time raster dataset is build from its name and its mapset
id = "test@PERMANENT"
# We create the temporal database interface for fast processing
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()

# We create a space time raster dataset object
strds = tgis.SpaceTimeRasterDataset(id)
# The id of a space time raster dataset is build from its name and its mapset
id = "test@PERMANENT"

# Check if the space time raster dataset is in the temporal database
if strds.is_in_db(dbif=dbif) is False:
dbif.close()
gs.fatal(_("Space time %s dataset <%s> not found") % (
strds.get_new_map_instance(None).get_type(), id))
# We create a space time raster dataset object
strds = tgis.SpaceTimeRasterDataset(id)

# Fill the object with the content from the temporal database
strds.select(dbif=dbif)
# Check if the space time raster dataset is in the temporal database
if strds.is_in_db(dbif=dbif) is False:
dbif.close()
gs.fatal(_("Space time %s dataset <%s> not found") % (
strds.get_new_map_instance(None).get_type(), id))

# Print information about the space time raster dataset to stdout
strds.print_info()
# Fill the object with the content from the temporal database
strds.select(dbif=dbif)

# Get all maps that are registered in the strds and print
# information about the maps to stdout
maps = strds.get_registered_maps_as_objects(dbif=dbif)
# Print information about the space time raster dataset to stdout
strds.print_info()

# We iterate over the temporal sorted map list
for map in maps:
# We fill the map object with the content
# from the temporal database. We use the existing
# database connection, otherwise a new connection
# will be established for each map object
# which slows the processing down
map.select(dbif=dbif)
map.print_info()
# Get all maps that are registered in the strds and print
# information about the maps to stdout
maps = strds.get_registered_maps_as_objects(dbif=dbif)

# Close the database connection
dbif.close()
# We iterate over the temporal sorted map list
for map in maps:
# We fill the map object with the content
# from the temporal database. We use the existing
# database connection, otherwise a new connection
# will be established for each map object
# which slows the processing down
map.select(dbif=dbif)
map.print_info()

# Close the database connection
dbif.close()


if __name__ == "__main__":
main()


Creation of a space time dataset
Expand All @@ -332,52 +351,58 @@ for different space time datasets (raster, 3D raster and vector):
import grass.temporal as tgis
import grass.script as gs

# The id of the new space time dataset
id="test@PERMANENT"
# The title of the new space time dataset
title="This is a test dataset"
# The description of the space time dataset
description="The description"
# The type of the space time dataset (strds, str3ds or stvds)
type="strds"
# The temporal type of the space time dataset (absolute or relative)
temporal_type="absolute"

# Make sure the temporal database exists
# and set the temporal GIS environment
tgis.init()

# We use the dataset factory to create an new space time dataset instance of a specific type
stds = tgis.dataset_factory(type, id)

# We need a dtabase connection to insert the content of the space time dataset
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()

# First we check if the dataset is already in the database
if stds.is_in_db(dbif=dbif) and overwrite is False:
dbif.close()
gs.fatal(_("Space time %s dataset <%s> is already in the database. "
"Use the overwrite flag.") %
(stds.get_new_map_instance(None).get_type(), name))

# We delete the exiting dataset and create a new one in case we are allowed to overwrite it
if stds.is_in_db(dbif=dbif) and overwrite is True:
gs.warning(_("Overwrite space time %s dataset <%s> "
"and unregister all maps.") %
(stds.get_new_map_instance(None).get_type(), name))
stds.delete(dbif=dbif)
stds = stds.get_new_instance(id)

# We set the initial values. This function also created the command history.
stds.set_initial_values(temporal_type=temporaltype, semantic_type="mean",
title=title, description=description)
def main():
# The id of the new space time dataset
id="test@PERMANENT"
# The title of the new space time dataset
title="This is a test dataset"
# The description of the space time dataset
description="The description"
# The type of the space time dataset (strds, str3ds or stvds)
type="strds"
# The temporal type of the space time dataset (absolute or relative)
temporal_type="absolute"

# Make sure the temporal database exists
# and set the temporal GIS environment
tgis.init()

# We use the dataset factory to create an new space time dataset instance of a specific type
stds = tgis.dataset_factory(type, id)

# We need a dtabase connection to insert the content of the space time dataset
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()

# First we check if the dataset is already in the database
if stds.is_in_db(dbif=dbif) and overwrite is False:
dbif.close()
gs.fatal(_("Space time %s dataset <%s> is already in the database. "
"Use the overwrite flag.") %
(stds.get_new_map_instance(None).get_type(), name))

# We delete the exiting dataset and create a new one in case we are allowed to overwrite it
if stds.is_in_db(dbif=dbif) and overwrite is True:
gs.warning(_("Overwrite space time %s dataset <%s> "
"and unregister all maps.") %
(stds.get_new_map_instance(None).get_type(), name))
stds.delete(dbif=dbif)
stds = stds.get_new_instance(id)

# We set the initial values. This function also created the command history.
stds.set_initial_values(temporal_type=temporaltype, semantic_type="mean",
title=title, description=description)

# Now we can insert the new space time dataset in the database
stds.insert(dbif=dbif)

# Close the database connection
dbif.close()

# Now we can insert the new space time dataset in the database
stds.insert(dbif=dbif)

# Close the database connection
dbif.close()
if __name__ == "__main__":
main()


Temporal shifting
Expand All @@ -388,27 +413,33 @@ Temporal shifting
import grass.script as gs
import grass.temporal as tgis

id="test@PERMANENT"
type="strds"

# Make sure the temporal database exists
tgis.init()
def main():
id="test@PERMANENT"
type="strds"

dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()
# Make sure the temporal database exists
tgis.init()

stds = tgis.dataset_factory(type, id)
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()

if stds.is_in_db(dbif) is False:
dbif.close()
gs.fatal(_("Space time dataset <%s> not found in temporal database") % (id))
stds = tgis.dataset_factory(type, id)

if stds.is_in_db(dbif) is False:
dbif.close()
gs.fatal(_("Space time dataset <%s> not found in temporal database") % (id))

stds.select(dbif=dbif)
stds.select(dbif=dbif)

stds.snap(dbif=dbif)

stds.update_command_string(dbif=dbif)
dbif.close()

stds.snap(dbif=dbif)

stds.update_command_string(dbif=dbif)
dbif.close()
if __name__ == "__main__":
main()


References
Expand Down
10 changes: 5 additions & 5 deletions python/grass/pygrass/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import grass.lib.gis as libgis
from grass.exceptions import FatalError
from grass.script.utils import _get_multiprocessing_context
from grass.script.utils import _environ_process, _get_multiprocessing_context

if TYPE_CHECKING:
from multiprocessing.connection import Connection
Expand Down Expand Up @@ -183,8 +183,8 @@ def __init__(self, raise_on_error: bool = False) -> None:
ctx = _get_multiprocessing_context()
self.client_conn, self.server_conn = ctx.Pipe()
self.lock = ctx.Lock()
self.server = ctx.Process(
target=message_server, args=(self.lock, self.server_conn)
self.server = _environ_process(
target=message_server, args=(self.lock, self.server_conn), ctx=ctx
)
self.server.daemon = True
self.server.start()
Expand All @@ -194,8 +194,8 @@ def start_server(self) -> None:
ctx = _get_multiprocessing_context()
self.client_conn, self.server_conn = ctx.Pipe()
self.lock = ctx.Lock()
self.server = ctx.Process(
target=message_server, args=(self.lock, self.server_conn)
self.server = _environ_process(
target=message_server, args=(self.lock, self.server_conn), ctx=ctx
)
self.server.daemon = True
self.server.start()
Expand Down
29 changes: 17 additions & 12 deletions python/grass/pygrass/modules/interface/module.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from multiprocessing import Process, Queue
import time
from xml.etree.ElementTree import fromstring

from grass.exceptions import CalledModuleError, GrassError, ParameterError
from grass.script.core import Popen, PIPE, use_temp_region, del_temp_region
from grass.script.utils import decode, resolve_nprocs
from grass.script.utils import (
_environ_process,
_get_multiprocessing_context,
decode,
resolve_nprocs,
)
from .docstring import docstring_property
from .parameter import Parameter
from .flag import Flag
Expand Down Expand Up @@ -914,7 +918,7 @@ class MultiModule:

>>> from grass.pygrass.modules import Module
>>> from grass.pygrass.modules import MultiModule
>>> from multiprocessing import Process
>>> from multiprocessing.process import BaseProcess
>>> import copy

Synchronous module run
Expand Down Expand Up @@ -952,7 +956,7 @@ class MultiModule:
... sync=False,
... )
>>> t = mm.run()
>>> isinstance(t, Process)
>>> isinstance(t, BaseProcess)
True
>>> m_list = mm.wait()
>>> m_list[0].returncode
Expand All @@ -978,7 +982,7 @@ class MultiModule:
>>> str(mm)
'g.region format=plain -p ; g.region format=plain -p ; g.region format=plain -p ; g.region format=plain -p ; g.region format=plain -p'
>>> t = mm.run()
>>> isinstance(t, Process)
>>> isinstance(t, BaseProcess)
True
>>> m_list = mm.wait()
>>> m_list[0].returncode
Expand Down Expand Up @@ -1015,7 +1019,8 @@ def __init__(self, module_list, sync=True, set_temp_region=False):
self.set_temp_region = set_temp_region
self.finish_ = sync # We use the same variable name a Module
self.p = None
self.q = Queue()
self.ctx = _get_multiprocessing_context()
self.q = self.ctx.Queue()

def __str__(self):
"""Return the command string that can be executed in a shell"""
Expand All @@ -1040,7 +1045,7 @@ def run(self):
be run one after the another.

:return: None in case of ``self.finish_`` is True,
otherwise a multiprocessing.Process object that invokes the modules
otherwise a multiprocessing process object that invokes the modules
"""

if self.finish_ is True:
Expand All @@ -1049,11 +1054,11 @@ def run(self):
module.run()
return None
if self.set_temp_region is True:
self.p = Process(
target=run_modules_in_temp_region, args=[self.module_list, self.q]
self.p = _environ_process(
run_modules_in_temp_region, (self.module_list, self.q), self.ctx
)
else:
self.p = Process(target=run_modules, args=[self.module_list, self.q])
self.p = _environ_process(run_modules, (self.module_list, self.q), self.ctx)
self.p.start()

return self.p
Expand All @@ -1074,7 +1079,7 @@ def wait(self):
def run_modules_in_temp_region(module_list, q):
"""Run the modules in a temporary region environment

This function is the argument for multiprocessing.Process class
This function is the target of the multiprocessing process
in the MultiModule asynchronous execution.

:param module_list: The list of modules to run in serial
Expand All @@ -1093,7 +1098,7 @@ def run_modules_in_temp_region(module_list, q):
def run_modules(module_list, q):
"""Run the modules

This function is the argument for multiprocessing.Process class
This function is the target of the multiprocessing process
in the MultiModule asynchronous execution.

:param module_list: The list of modules to run in serial
Expand Down
Loading
Loading