I have a multithreaded client which starts off as shown below:
pauseEvent = threading.Event()
input = InputThread(clientSocket, username, pauseEvent)
response = ReponseThread(clientSocket, username, pauseEvent)
input.start()
response.start()
Passing in the threading.Event() into the threads which then use it as shown:
InputThread
while True:
message = input("blah blah")
socket.send(message.encode())
print("paused")
self.pauseEvent.wait()
ResponseThread
while True:
data = self.clientSocket.recv(2048)
response: str = data.decode()
print("\n" + response[22:])
sys.stdout.flush()
self.pauseEvent.set()
So unless i'm missing something inputThread should be waiting for input while ResponseThread is waiting for recv, after an input is given in stdin, the input thread sends the message, then waits until response thread receives and prints the response when it can then 'unlock' the event. After which the input thread will then reprompt.
Expected flow:
- blah blah
- { user input }
- server response
repeat
Current flow:
- blah blah
- { user input }
- blah blah
- server response
- { user input }
I tried dumping the stout buffer as can be seen with the sys.stdout.flush(), but this did nothing. I also tried using python 3.9.6 (as this is the only other python interpreter I have downloaded) and for some strange reason this makes it work as expected. Not sure if there was a big change to threading events since that release or not.