While developing cd_burn plugin i needed to read child output in
non-blocking mode in order to parse the output of the child app in
realtime without locking my thread.
After a few attempts i finally got it. Here is the "recipe" for who ever
needs it:

import fcntl, os
def run_my_child(self,cmd=None,cwd=None,wait=0):

      cmd = cmd + " 2>&1"
      child_app = util.popen3.Popen3(cmd,cwd=cwd)

      if wait:
              self.makeNonBlocking(child_app.fromchild.fileno())

              while child_app.poll() < 0:
                      try:
                       while 1:
                              line = child_app.fromchild.readline()
                              if line:
                               print line
                              else:
                               break
                      except IOError:
                       #the fd was not ready to read
                       pass

                      time.sleep(1)
      return child_app

  def makeNonBlocking(self,fd):
      fl = fcntl.fcntl(fd, fcntl.F_GETFL)
      try:
              fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NDELAY)
      except AttributeError:
              fcntl.fcntl(fd, fcntl.F_SETFL, fl | fcntl.FNDELAY)





-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
_______________________________________________
Freevo-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-devel

Reply via email to