Note that there are some explanatory texts on larger screens.

plurals
  1. POstdout Won't Flush After Exception
    primarykey
    data
    text
    <p>I have the following Python code:</p> <pre><code>import sys import traceback fifo_in = sys.argv[1] while 1: try: exec open(fifo_in) except: traceback.print_exc() sys.stdout.flush() </code></pre> <p>The first argument is a named pipe created by <code>mkfifo</code>. So the following prints '1':</p> <pre><code>mkfifo input python script.py input ... in a separate terminal ... echo "print 1" &gt; input </code></pre> <p>Great, so far so good. But when I do something like <code>echo "foobar" &gt; input</code>, the script only prints <em>part</em> of the traceback. It then pauses until I send it another command, and the output gets all mixed up:</p> <pre><code>echo "asdf" &gt; input # pause here and check output echo "print 1" &gt; input ... in output terminal ... Traceback (most recent call last): File "test.py", line 8, in &lt;module&gt; exec open(fifo_in) File "in", line 1, in &lt;module&gt; ...PAUSES HERE... print 1 NameError: name 'asdf' is not defined </code></pre> <p>What's going on? How can I get stdout to flush fully and why is it out of order? I've tried using <code>traceback.format_exc</code> instead, then printing it by hand, but I get the same result. Calling <code>sys.stderr.flush</code> does not fix anything either. I've also tried putting a sleep in the loop to see if that helps, but nothing.</p> <p><strong>UPDATE</strong></p> <p>One interesting piece of behavior I am seeing: If I <code>ctrl+c</code> it, normally the program keeps running - the try/except just catches the <code>KeyboardInterrupt</code> and it keeps looping. However, if I <code>ctr+c</code> it after sending it an error, the program exits and I get the following. It's almost like it pauses inside of <code>print_exc</code>:</p> <pre><code>^CTraceback (most recent call last): File "test.py", line 10, in &lt;module&gt; traceback.print_exc() File "/usr/lib/python2.7/traceback.py", line 232, in print_exc print_exception(etype, value, tb, limit, file) File "/usr/lib/python2.7/traceback.py", line 125, in print_exception print_tb(tb, limit, file) File "/usr/lib/python2.7/traceback.py", line 69, in print_tb line = linecache.getline(filename, lineno, f.f_globals) File "/usr/lib/python2.7/linecache.py", line 14, in getline lines = getlines(filename, module_globals) File "/usr/lib/python2.7/linecache.py", line 40, in getlines return updatecache(filename, module_globals) File "/usr/lib/python2.7/linecache.py", line 132, in updatecache with open(fullname, 'rU') as fp: KeyboardInterrupt </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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