Does pexpect.spawn take time to return to its calling Python script?
I'm writing a script to manage a Karaf container, using pexpect to inject commands. If Karaf isn't running, I've noticed that I cannot always reliably use isalive()
unless I insert a pause in the script.
For example:-
# open a Karaf SSH session
karaf_session = pexpect.spawn("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p " +
karaf_port + " " +
karaf_id + "@" +
karaf_host)
log_file = open(log_file_name, "wb")
karaf_session.logfile_read = log_file
time.sleep(1)
if karaf_session.isalive():
print("Connected to Karaf")
else:
print("Failed to connect to Karaf")
If I comment out time.sleep(1)
then isalive()
returns true, even though Karaf is not running.
Depending on whether the logfile_read
is before or after the sleep, it either catches nothing or the following:-
ssh: connect to host localhost port 8101: Connection refused
I'd rather have something more reliable than an arbitrary sleep!