Monitor dict of hosts to popen objects a line at a time timeoutms: timeout for poll() readline: return single line of output yields: host, line/output (if any) terminates: when all EOFs received
(popens, timeoutms=500, readline=True,
readmax=1024 )
| 453 | # Popen support |
| 454 | |
| 455 | def pmonitor(popens, timeoutms=500, readline=True, |
| 456 | readmax=1024 ): |
| 457 | """Monitor dict of hosts to popen objects |
| 458 | a line at a time |
| 459 | timeoutms: timeout for poll() |
| 460 | readline: return single line of output |
| 461 | yields: host, line/output (if any) |
| 462 | terminates: when all EOFs received""" |
| 463 | poller = poll() |
| 464 | fdToHost = {} |
| 465 | fdToDecoder = {} |
| 466 | for host, popen in popens.items(): |
| 467 | fd = popen.stdout.fileno() |
| 468 | fdToHost[ fd ] = host |
| 469 | fdToDecoder[ fd ] = getincrementaldecoder() |
| 470 | poller.register( fd, POLLIN ) |
| 471 | flags = fcntl( fd, F_GETFL ) |
| 472 | fcntl( fd, F_SETFL, flags | O_NONBLOCK ) |
| 473 | # pylint: disable=too-many-nested-blocks |
| 474 | while popens: |
| 475 | fds = poller.poll( timeoutms ) |
| 476 | if fds: |
| 477 | for fd, event in fds: |
| 478 | host = fdToHost[ fd ] |
| 479 | decoder = fdToDecoder[ fd ] |
| 480 | popen = popens[ host ] |
| 481 | if event & ( POLLIN | POLLHUP ): |
| 482 | while True: |
| 483 | try: |
| 484 | f = popen.stdout |
| 485 | line = decoder.decode( f.readline() if readline |
| 486 | else f.read( readmax ) ) |
| 487 | except IOError: |
| 488 | line = '' |
| 489 | if line == '': |
| 490 | break |
| 491 | yield host, line |
| 492 | if event & POLLHUP: |
| 493 | poller.unregister( fd ) |
| 494 | del popens[ host ] |
| 495 | else: |
| 496 | yield None, '' |
| 497 | |
| 498 | # Other stuff we use |
| 499 | def sysctlTestAndSet( name, limit ): |
no test coverage detected