title: Python pexpect - spawn takes time to establish
body: <p>Does pexpect.spawn take time to return to its calling Python script?</p>
  
  <p>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 <code>isalive()</code> unless I insert a pause in the script.</p>
  
  <p>For example:-</p>
  
  <pre><code># 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")
  </code></pre>
  
  <p>If I comment out <code>time.sleep(1)</code> then <code>isalive()</code>returns true, even though Karaf is not running.</p>
  
  <p>Depending on whether the <code>logfile_read</code> is before or after the sleep, it either catches nothing or the following:-</p>
  
  <pre><code>ssh: connect to host localhost port 8101: Connection refused
  </code></pre>
  
  <p>I'd rather have something more reliable than an arbitrary sleep!</p>