You have to syncronize your input with the thread output preventing them from happening at the same time.
You can modify the main loop like:
lock = threading.lock()
while 1:
raw_input() # Waiting for you to press Enter
with lock:
r = raw_input('--> ')
# send your command to the thread
And then lock the background thread printing:
def worker(lock, ...):
[...]
with lock:
print('what the thread write')
In short when you Press Enter
you will stop the thread and enter in "input mode".
To be more specific, every time you Press Enter
you will:
- wait for the lock to be available
- acquire the lock
- print
-->
and wait for your command
- insert your command
- send that command to the thread
- release the lock
So your thread will be stopped only if it tries to print when you are in "input mode",
and in your terminal you'll get something like:
some previous output
---> your input
THE THREAD OUTPUT