I have a python script which uses pexpect and I want to timeout (i.e. raise
pexpect.TIMEOUT) if a long running command does not produce the output I am
expecting. To simulate the 'long running command', consider the following
example which simply runs the 'yes' command which prints an endless series
of 'y' characters to the terminal. I want to timeout after 10 seconds -:
child=pexpect.spawn('ssh me@remote_host')
child.sendline('yes')
child.expect([pexpect.TIMEOUT, "the pattern I'm expecting"],10)
In this situation, pexpect.TIMEOUT will never be raised. The TIMEOUT
exception is only raised if *no* output is received or the output has
completed and the pattern expected is not matched within the timeout
period. But what about situations where a command produces an infinite
amount of output? In the above example, the child.expect statement will
hang forever.
I thought about using signal.signal to set an alarm that fires a few seconds
after timeout and explicitly closes the session. However, my application
is multi-threaded (i.e. each thread respresents a connection to a remote
host) and signals can not be used outside the main thread :-(
Any ideas?
Cheers.
Adrian Casey.