Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is Pexpect intermittently hanging (not detecting EOF) after executing certain commands?
    text
    copied!<p><strong>Context:</strong></p> <p>I have some code written using <code>pexpect</code>, whose job is to give "live" output of a command. I.e. print something out when a command generates some output, or soon after, rather than waiting until the command completes and then interacting with its output. </p> <p>All I'm doing is starting and stopping a service. I do this by <code>spawn</code>ing the process, and then outputting each line as it is printed, like so:</p> <pre><code>def watch(process): output = "" while True: try: line = process.read_nonblocking(timeout = -1) print(line, end ="") output += line except pexpect.EOF: break del process return output while True: print("IN 1") process = pexpect.spawn("service",["zend-server", "stop"], timeout = None) watch(process) print("OUT 1") print("IN 2") process = pexpect.spawn("service",["zend-server", "start"], timeout = None) watch(process) print("OUT 2") </code></pre> <p>This code should just loop the service: start it and stop it over and over, printing the output of the start/stop as it goes. It prints the output fine. However, it eventually hangs right before "OUT 2". I can view the output, and see the <code>service</code> call stop its execution. The <code>watch</code> function never raises an EOF and exits, however. </p> <p>This doesn't happen with every service. Some services loop indefinitely. <code>zend-server</code>, however, along with a few other unrelated commands, fail intermittently in the same way. </p> <p>By "eventually hangs", I mean that it starts/stops the service a few (variable on each run) times, and hangs. It usually gums up after 4-6, though never on the first call--always at least on the second (hence the <code>del</code> statement; I figured I'd play it safe). </p> <p>Python 2.6.6, CentOS (64) 6.3, Pexpect 2.3-6, FWIW</p> <p><strong>Question:</strong></p> <p>Why is <code>pexpect</code> hanging on certain commands? How should I resolve this issue? Using timeouts isn't a feasible solution, as some of these commands really can run for an arbitrarily long time. <code>service zend-server stop</code> is just the one I picked for an example because it doesn't take that long, and I can observe it finishing. </p> <p><strong>What I've Tried:</strong></p> <p>I've tried replacing the <code>watch</code> method with the following, which uses <code>expect('\n')</code>, but the results are the same: a variable number of restarts, and then an eventual hang. </p> <p>I can also add <code>pexpect.EOF</code> into the array that is <code>expect</code>ed along with <code>\n</code>, and handle the return value to break out of the loop, it still hangs in the same place. </p> <pre><code>def watch2(process): output = "" done = False while True: try: if not process.isalive(): line = process.readline() done = True else: process.expect(['\n']) line = process.before print(line) output += line if done: raise pexpect.EOF(0) except pexpect.EOF: break del process return output </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload