I have a thread which can print some text on the console and the main program have a raw_input
to control the thread.
My problem is when I'm writing and the thread too I get something like this:
-->whatiwWHATTHETHREADWRITErite
but I would like to get some thing like this
WHATTHETHREADWRITE
-->whatiwrite
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:
-->
and wait for your commandSo 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