From 31fe4f1bd0f915d8f891974734bd9227d59d8af0 Mon Sep 17 00:00:00 2001 From: Bob Lantz Date: Wed, 6 Mar 2013 17:26:52 -0800 Subject: [PATCH] Fix pmonitor() to not return blank lines on EOF fixes #109 (hopefully) --- mininet/util.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/mininet/util.py b/mininet/util.py index ff75b10..2461210 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -4,7 +4,7 @@ from mininet.log import output, info, error, warn from time import sleep from resource import setrlimit, RLIMIT_NPROC, RLIMIT_NOFILE -from select import poll, POLLIN +from select import poll, POLLIN, POLLHUP from subprocess import call, check_call, Popen, PIPE, STDOUT import re from fcntl import fcntl, F_GETFL, F_SETFL @@ -326,27 +326,24 @@ def pmonitor(popens, timeoutms=500, readline=True, # Use non-blocking reads flags = fcntl( fd, F_GETFL ) fcntl( fd, F_SETFL, flags | O_NONBLOCK ) - while True: + while popens: fds = poller.poll( timeoutms ) if fds: - for fd, _event in fds: + for fd, event in fds: host = fdToHost[ fd ] popen = popens[ host ] - if readline: - # Attempt to read a line of output - # This blocks until we receive a newline! - line = popen.stdout.readline() - else: - line = popen.stdout.read( readmax ) - yield host, line + if event & POLLIN: + if readline: + # Attempt to read a line of output + # This blocks until we receive a newline! + line = popen.stdout.readline() + else: + line = popen.stdout.read( readmax ) + yield host, line # Check for EOF - if not line: - popen.poll() - if popen.returncode is not None: - poller.unregister( fd ) - del popens[ host ] - if not popens: - return + elif event & POLLHUP: + poller.unregister( fd ) + del popens[ host ] else: yield None, ''