deleting printed line from screen
<p>I have code that runs for various lengths of time, during which a thread runs and displays a spinning cursor. When the code finishes it's task it prints some output, sets an event and the cursor stops. However this leaves the last character that was printed on the screen. I would like this to be removed.</p>

<pre><code>def spinning_cursor(event):                                                    
    for j in itertools.cycle('/-\|'):                                           
        if not event.is_set():                                                 
            sys.stdout.write(j)                                                
            sys.stdout.flush()                                      
            time.sleep(0.1)                                                    
            sys.stdout.write('\b')                                              
        else:  
            #sys.stdout.write('\b')                                                                                                                   
            return  
</code></pre>

<p>What I see on screen looks like this:</p>

<pre><code>paul#
Writing file...
/
info: file has been written
paul# 
</code></pre>

<p>I want the <code>/</code> to disappear when the event is set. I have tried  <code>sys.stdout.write('\b')</code> before returning (uncommenting the line above) but this code seems to run after the subsequent prints in the main thread have, and it deletes nothing. Why is this? A more obvious example where I use <code>sys.stdout.write('TEST')</code> before returning in the <code>else</code>.</p>

<pre><code>paul#
Writing file...
/
info: file has been written
TESTpaul# 
</code></pre>

<p>I have found a solution via moving up one line before prints in the main thread but as there are many code paths it is messy:</p>

<pre><code>CURSOR_UP_ONE = '\x1b[1A'                                                                                          
sys.stdout.write(CURSOR_UP_ONE)
</code></pre>

<p>Here is how the spinning cursor is called. Any ideas?</p>

<pre><code>def copy():                                                                                                                                                                                                                                                   
    processing_flag = Event()                                                                                                                                                                                                                                                        
    Thread(target = spinning_cursor, kwargs = {'event': processing_flag}).start()                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    try:                                                                                                                                                                                                                                                                         
        ...                                                                                                                                                                                                                                                  
    except: 
        ...                                                                                                                                                                                                                                                             
    finally:                                                                                                                                                                                                                                                                     
        processing_flag.set()                                                                                                                                                                                                                                                    
</code></pre>

<p>The problem is that the main thread does not wait for the <em>spinning_cursor</em> one. So here is what happens (I could reproduce it):</p>

<pre><code>main                                     | spinning
---------------------------------------------------------------------------
starts spinning thread                   | starts
works                                    | displays a *spinning* cursor
ends working                             | ==
sets event                               | waits for running time
continues and writes to stdout  (*)      | may be still waiting ...  
...                                      | writes its last backspace and exits (*)
</code></pre>

<p>(*) I could prove that the main thread begun writing on stdout even with a <code>write</code> occuring after the <code>finaly</code> block <strong>before</strong> the spinning thread can write its last backspace by writing a printable character at the end of the thread. That character was printed too late.</p>

<p>How to fix: simply <code>join</code> the <em>spinning_cursor</em> thread:</p>

<pre><code>def copy():
    processing_flag = Event()
    t = Thread(target = spinning_cursor, kwargs = {'event': processing_flag})
    t.start()
    try:
        ...
    except:
        ...
    finally:
        processing_flag.set()
        t.join() # waits for the thread to output its last backspace
</code></pre>

python multithreading