By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

pexpect: TIMEOUT exception never raised

P: n/a
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.
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.