Input() called in a thread, and it blocked the main thread if the main thread called input()

hi experts,
what i want to do in my program is to listen to the input from the keyboard while the main thread is running.
my solution is to create a thread to listen to the input.
while True:
keyboard_input = input()
# some logic after the input detected.

meanwhile in the main thread, in some cases, i need the user input something from the keyboard either.
yes_or_no = input(“Do you want … (y/n)”)

when the program runs to the yes_or_no, it will be blocked by the keyboard_input.

is there any way to avoid this?

I’m facing the same issue. If you found a solution to this, please help.

It’s not 100% clear what you want, but a likely solution would be to have a single threading.Lock which you take while you get input, like this:

import threading

INPUT_LOCK = threading.Lock()

def locked_input(prompt):
    with INPUT_LOCK:
        return input(prompt)

This means that exactly one input statement can be active at one time, and if they happen at the “same time”, the second input statement will go off after the first one is totally complete.

Hope this helps!