def pmonitor(popens, timeoutms=500, readline=True, readmax=1024):
 poller = poll()
 fdToHost = {}
 for (host, popen) in popens.iteritems():
  fd = popen.stdout.fileno()
  fdToHost[fd] = host
  poller.register(fd, POLLIN)
  if (not readline):
   flags = fcntl(fd, F_GETFL)
   fcntl(fd, F_SETFL, (flags | O_NONBLOCK))
 while popens:
  fds = poller.poll(timeoutms)
  if fds:
   for (fd, event) in fds:
    host = fdToHost[fd]
    popen = popens[host]
    if (event & POLLIN):
     if readline:
      line = popen.stdout.readline()
     else:
      line = popen.stdout.read(readmax)
     (yield (host, line))
    elif (event & POLLHUP):
     poller.unregister(fd)
     del popens[host]
  else:
   (yield (None, ''))

def pmonitor ( popens , timeoutms = 500 , readline = True , readmax = 1024 ) : poller = poll ( ) fdToHost = { } for ( host , popen ) in popens . iteritems ( ) : fd = popen . stdout . fileno ( ) fdToHost [ fd ] = host poller . register ( fd , POLLIN ) if ( not readline ) : flags = fcntl ( fd , F_GETFL ) fcntl ( fd , F_SETFL , ( flags | O_NONBLOCK ) ) while popens : fds = poller . poll ( timeoutms ) if fds : for ( fd , event ) in fds : host = fdToHost [ fd ] popen = popens [ host ] if ( event & POLLIN ) : if readline : line = popen . stdout . readline ( ) else : line = popen . stdout . read ( readmax ) ( yield ( host , line ) ) elif ( event & POLLHUP ) : poller . unregister ( fd ) del popens [ host ] else : ( yield ( None , '' ) )
monitor dict of hosts to popen objects a line at a time timeoutms: timeout for poll() readline: return single line of output yields: host .