[GRASS-dev] Continuos output from grass python script of subprocess

Michel Wortmann wortmann at pik-potsdam.de
Sun Aug 10 05:16:33 PDT 2014


Hi grass-devs,
could you tell me why this simple example script doesn’t show me the output of my program that I’m launching through subprocess? It’s only giving me the output once the program has finished. As the program runs for quite some time, I would like to see the output while the script is running through the grass module interface. (In the actual script, I’m preparing some input and postprocess output before/after the program has run.)

Any hints would be highly appreciated.
Thanks and best regards,
Michel


#!/usr/bin/env python

#%module
#% description: Testing
#% keywords: raster, statistics
#%end

#%option
#% key: myprogram
#% type: string
#% key_desc: name
#% description: Any long running program with continous output to stdout
#%end

import sys
import subprocess

import grass.script as grass

def main():
    # put code here
    p = subprocess.Popen(options['myprogram'], shell=True, 
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    while True:
        nextline = p.stdout.readline()
        if nextline == '' and p.poll() != None:
            break
        grass.message(nextline)
    sys.stdout.flush()
    return 0

if __name__ == "__main__":
        
    # normal start
    options, flags = grass.parser()
    sys.exit(main())


More information about the grass-dev mailing list