0

I have the following code, which lets me print and receive keyboard input at the same time:

import time
from threading import Thread

def InpThread():
    while True:
        response = input("Type in here\r\n")
        print("Response is " + response)

tinp = Thread(target=InpThread)
tinp.start()

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

I've stripped this down to a simple case, my case has other stuff but the problem is reproducible for me with this example. Last week, I had no problems with the return of input() giving me all characters entered till that point. Now, it appears every call of print() in the other thread wipes all characters away, so that mostly I am left with an empty input, unless I time it right and am very fast.

Printing
aPrinting
b
Response is b
Type in here
Printing

I am using Python interpreter 3.5.2.

I cannot see any change in my code over the last week, all I can think of is that Windows has updated, or perhaps the python module.

How can I keep all input and not have it wiped by a call to print()?

1 Answers1

0

You should probably use lock on the print function.

Check This answer. It creates a lock object and uses it later to print.

Community
  • 1
  • 1
aliqandil
  • 1,673
  • 18
  • 28