Skip to content
Snippets Groups Projects
Commit f24e70a4 authored by Bob Lantz's avatar Bob Lantz
Browse files

Added timeout option for monitor().

This allows monitor() to be used for polling. Of course, you
probably want to use poll() first anyway, but it's nice not
to have to block if there's no output.
parent 3482b446
No related branches found
No related tags found
No related merge requests found
......@@ -149,10 +149,11 @@ def stop( self ):
"Stop node."
self.terminate()
def waitReadable( self ):
"Wait until node's output is readable."
def waitReadable( self, timeoutms=None ):
"""Wait until node's output is readable.
timeoutms: timeout in ms or None to wait indefinitely."""
if len( self.readbuf ) == 0:
self.pollOut.poll()
self.pollOut.poll( timeoutms )
def sendCmd( self, cmd, printPid=True ):
"""Send a command, followed by a command to echo a sentinel,
......@@ -160,6 +161,9 @@ def sendCmd( self, cmd, printPid=True ):
assert not self.waiting
if isinstance( cmd, list ):
cmd = ' '.join( cmd )
if not re.search( r'\w', cmd ):
# Replace empty commands with something harmless
cmd = 'echo -n'
if len( cmd ) > 0 and cmd[ -1 ] == '&':
separator = '&'
cmd = cmd[ :-1 ]
......@@ -180,11 +184,12 @@ def sendInt( self, sig=signal.SIGINT ):
except Exception:
pass
def monitor( self ):
def monitor( self, timeoutms=None ):
"""Monitor and return the output of a command.
Set self.waiting to False if command has completed."""
Set self.waiting to False if command has completed.
timeoutms: timeout in ms or None to wait indefinitely."""
assert self.waiting
self.waitReadable()
self.waitReadable( timeoutms )
data = self.read( 1024 )
# Look for PID
marker = chr( 1 ) + r'\d+\n'
......@@ -395,10 +400,10 @@ def sendCmd( self, cmd, printPid=False):
error( '*** Error: %s has execed and cannot accept commands' %
self.name )
def monitor( self ):
"Monitor node."
def monitor( self, *args, **kwargs ):
"Monitor a switch."
if not self.execed:
return Node.monitor( self )
return Node.monitor( self, *args, **kwargs )
else:
return True, ''
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment