Skip to content

Instantly share code, notes, and snippets.

@erget
Created August 7, 2012 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erget/3282580 to your computer and use it in GitHub Desktop.
Save erget/3282580 to your computer and use it in GitHub Desktop.
Elegant parallelization in GRASS
#!/usr/bin/env python
# This script is derived from GPL 2+ code
# Cudos to Hamish for providing the example it's based on.
# Also see the following link for an example of parallelization
# using the multiprocessing library.
# https://trac.osgeo.org/grass/browser/grass/trunk/scripts/i.landsat.rgb/i.landsat.rgb.py
import os
import grass.script as grass
# Have user set number of workers. Here an example of 4 has been set
workers = 4
# This is only a set of examples for r.slope.aspect jobs where the maps are
# named serially.
jobs = range(20)
# Check if workers are already being used
if workers is 1 and "WORKERS" in os.environ:
workers = int(os.environ["WORKERS"])
if workers < 1:
workers = 1
# Initialize process dictionary
proc = {}
# Loop over jobs
for i in range(jobs):
# Insert job into dictinoary to keep track of it
proc[i] = grass.start_command('r.slope.aspect',
elevation='elev_' + str(i),
slope='slope_' + str(i))
# If the workers are used up, wait for all of them from the last group to
# finish.
if i % workers is 0:
for j in range(workers):
proc.[i - j].wait()
# Make sure all workers are finished.
for i in range(jobs):
if proc[i].wait() is not 0:
grass.fatal(_('Problem running analysis on evel_' + str(i) + '.')
@HamishB
Copy link

HamishB commented Aug 7, 2012

(note to other githubbers: this is derived from GPL 2+ code --Hamish)

@neteler
Copy link

neteler commented Jun 16, 2015

For the record: an example for the actual way of using "multiprocessing" in GRASS GIS Python scripts is available here: https://trac.osgeo.org/grass/browser/grass/trunk/scripts/i.colors.enhance/i.colors.enhance.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment