[GRASS-dev] Re: python scripts and standard input

Glynn Clements glynn at gclements.plus.com
Sat May 15 14:46:37 EDT 2010

Massimo Di Stefano wrote:

> the work-around :
> > while 1:
> >   try:
> >       function() 
> >       break
> >   except IOError: 
> >       sleep(0.1)
> works, 
> but i have to add it for each grass command i'm running in the code.

First, I'd suggest checking to see whether this has been fixed in a
later version of Python. Or that the Python developers are at least
aware of it. Are you using the version from python.org?

If you can't get a correctly-functioning version of Python, I'd
suggest using a wrapper rather than writing the loop out repeatedly,

	def fixed(func, *args, **kwargs):
	    while True:
	            return func(*args, **kwargs)
	        except IOError:

	def fix(func):
	    def f(*args, **kwargs):
	        return fixed(func, *args, **kwargs)
	    return f

Then you can use e.g.:

	read = fix(read)

to create a fixed version of the function. Similarly for methods:

	MyType.my_method = fix(MyType.my_method)

Or you can wrap individual calls, replacing "func(...)" with
"fixed(func, ...)".

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-dev mailing list