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

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?

giovanni

http://grass.osgeo.org/programming7/percent_8c.html
http://grass.osgeo.org/programming7/namespacepython_1_1core.html#a14ce86fd0c5c18da189e6e3d0cc10501

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
  p.wait()

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@gclements.plus.com>

On Thu, Dec 1, 2011 at 7:57 AM, Glynn Clements <glynn@gclements.plus.com> wrote:
...

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\[&#39;GRASS\_MESSAGE\_FORMAT&#39;\] = &#39;gui&#39;
   p = grass\.start\_command\(\.\.\., stderr = grass\.PIPE, env = env\)
   \# read from p\.stderr
   p\.wait\(\)

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.

Thanks, added to
http://grass.osgeo.org/wiki/GRASS_and_Python#Percentage_output_for_progress_of_computation

Markus

Thanks very much! :wink:

giovanni

2011/12/1 Markus Neteler <neteler@osgeo.org>

On Thu, Dec 1, 2011 at 7:57 AM, Glynn Clements <glynn@gclements.plus.com> wrote:

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

p.wait()

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.

Thanks, added to
http://grass.osgeo.org/wiki/GRASS_and_Python#Percentage_output_for_progress_of_computation

Markus

Glynn Clements wrote:

  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
  p.wait()

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.

Or: redirect both stdout and stderr to the same pipe (and hope that
the normal output doesn't include anything which will be mistaken for
progress/error/etc messages):

  p = grass.start_command(..., stdout = grass.PIPE, stderr = grass.STDOUT, env = env)

--
Glynn Clements <glynn@gclements.plus.com>