One of threads rewrites console input in Python
<p>I have a problem with console app with threading. In first thread i have a function, which write symbol "x" into output. In second thread i have function, which waiting for users input. (Symbol "x" is just random choice for this question).</p>

<p>For ex.</p>

<p>Thread 1:</p>

<pre>

    while True:
        print "x"
        time.sleep(1)

</pre>

<p>Thread 2:</p>

<pre>

    input = null
    while input != "EXIT":
        input = raw_input()
        print input

</pre>

<p>But when i write text for thread 2 to console, my input text (for ex. HELLO) is rewroted. </p>

<pre>

    x
    x
    HELx
    LOx
    x
    x[enter pressed here]

    HELLO
    x
    x

</pre>

<p>Is any way how can i prevent rewriting my input text by symbol "x"?</p>

<p>Thanks for answers.</p>

<p>In a console, standard output (produced by the running program(s)) and standard input (produced by your keypresses) are both sent to screen, so they may end up all mixed.</p>

<p>Here your thread 1 writes 1 <code>x</code> by line every second, so if your take more than 1 second to type <code>HELLO</code> then that will produce the in-console output that you submitted.</p>

<p>If you want to avoid that, a few non-exhaustive suggestions:</p>

<ul>
<li><p>temporarily interrupt thread1 output when a keypress is detected</p></li>
<li><p>use a library such as ncurses to create separates zones for your program output and the user input</p></li>
<li><p>just suppress thread1 input, or send it to a file instead.</p></li>
</ul>

python multithreading console-application