pexpect: TIMEOUT exception never raised


Adrian Casey

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 [email protected]_host')
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?

Adrian Casey.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question