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.
This commit is contained in:
+14
-9
@@ -149,10 +149,11 @@ class Node( object ):
|
|||||||
"Stop node."
|
"Stop node."
|
||||||
self.terminate()
|
self.terminate()
|
||||||
|
|
||||||
def waitReadable( self ):
|
def waitReadable( self, timeoutms=None ):
|
||||||
"Wait until node's output is readable."
|
"""Wait until node's output is readable.
|
||||||
|
timeoutms: timeout in ms or None to wait indefinitely."""
|
||||||
if len( self.readbuf ) == 0:
|
if len( self.readbuf ) == 0:
|
||||||
self.pollOut.poll()
|
self.pollOut.poll( timeoutms )
|
||||||
|
|
||||||
def sendCmd( self, cmd, printPid=True ):
|
def sendCmd( self, cmd, printPid=True ):
|
||||||
"""Send a command, followed by a command to echo a sentinel,
|
"""Send a command, followed by a command to echo a sentinel,
|
||||||
@@ -160,6 +161,9 @@ class Node( object ):
|
|||||||
assert not self.waiting
|
assert not self.waiting
|
||||||
if isinstance( cmd, list ):
|
if isinstance( cmd, list ):
|
||||||
cmd = ' '.join( cmd )
|
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 ] == '&':
|
if len( cmd ) > 0 and cmd[ -1 ] == '&':
|
||||||
separator = '&'
|
separator = '&'
|
||||||
cmd = cmd[ :-1 ]
|
cmd = cmd[ :-1 ]
|
||||||
@@ -180,11 +184,12 @@ class Node( object ):
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def monitor( self ):
|
def monitor( self, timeoutms=None ):
|
||||||
"""Monitor and return the output of a command.
|
"""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
|
assert self.waiting
|
||||||
self.waitReadable()
|
self.waitReadable( timeoutms )
|
||||||
data = self.read( 1024 )
|
data = self.read( 1024 )
|
||||||
# Look for PID
|
# Look for PID
|
||||||
marker = chr( 1 ) + r'\d+\n'
|
marker = chr( 1 ) + r'\d+\n'
|
||||||
@@ -395,10 +400,10 @@ class Switch( Node ):
|
|||||||
error( '*** Error: %s has execed and cannot accept commands' %
|
error( '*** Error: %s has execed and cannot accept commands' %
|
||||||
self.name )
|
self.name )
|
||||||
|
|
||||||
def monitor( self ):
|
def monitor( self, *args, **kwargs ):
|
||||||
"Monitor node."
|
"Monitor a switch."
|
||||||
if not self.execed:
|
if not self.execed:
|
||||||
return Node.monitor( self )
|
return Node.monitor( self, *args, **kwargs )
|
||||||
else:
|
else:
|
||||||
return True, ''
|
return True, ''
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user