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

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.

Il giorno 15/mag/2010, alle ore 00.22, massimodisasha@gmail.com ha scritto:

Il giorno 30/apr/2010, alle ore 02.39, Glynn Clements ha scritto:

Massimo Di Stefano wrote:

actually i'm using :

out = subprocess.Popen(['m.proj', '-o'], stdout=subprocess.PIPE, stdin=subprocess.PIPE).communicate("%s %s" % (x,y))[0]

That will work on Unix, but Windows will complain due to the lack of
the .exe suffix. You can use grass.Popen() to get around that.

thanks Glynn i adopted this way and it works (but i haven't tested on windows)

This topic, maybe is good to discuss a problem i'm having about subprocess,
please apologize me if it is OT, tell me to open a new thread.

The error i'm having is strictly connected with subprocess and how grass handle it.
The weird is that the error i'm having comes up only on OSX while on linux all works fine.

this the error log :

File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 663, in communicate
stdout = self.stdout.read()
IOError: [Errno 4] Interrupted system call

This appears to be a bug either in Python's read() method for pipes or
in Python's subprocess module. The underlying read() is failing with
EINTR, which can happen if a signal is received during a blocking
system call.

Normally, you would configure the signal handler to resume any system
calls (unless you specifically want it to interrupt blocking calls,
e.g. implementing a timeout by setting a timer and having SIGALRM
interrupt the system call).

In this case, I would expect the code to just try again, rather than
raising an exception. EINTR and EAGAIN are "transient" errors, in that
a subsequent attempt will typically succeed.

i'm tring to find a way to follow your suggestions.

if "function()" is the subprocess action that give me error on osx

maybe i can try something like :

while 1:
  try:
      function()
      break
  except IOError:
      sleep(0.1)

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,
e.g.:

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

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