[GRASS-dev] obtain the progress percentage of a command run from python

Glynn Clements glynn at gclements.plus.com
Thu Dec 1 01:57:09 EST 2011

G. Allegri wrote:

> I've seen that the grass.script.core.percent() module method wraps the
> g.message -p command.
> It's ok when the work is done within the python code, but how to grab the
> progress emitted by a GRASS command (i,e, G_percent)?
> There are many ways to run a command from python. What is the most
> appropriate to obtain/pipe the percentage from the command... given it can
> be done?

If you want to parse the output, set GRASS_MESSAGE_FORMAT=gui in the
environment when running the command and read from the command's
stderr; e.g.

	import grass.script as grass
	env = os.environ.copy()
	env['GRASS_MESSAGE_FORMAT'] = 'gui'
	p = grass.start_command(..., stderr = grass.PIPE, env = env)
	# read from p.stderr

If you need to capture both stdout and stderr, you need to use
threads, select, or non-blocking I/O to consume data from both streams
as it is generated in order to avoid deadlock.

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-dev mailing list