Answer the question
In order to leave comments, you need to log in
Python how to remove delays in threads?
I am making a program that is turned on and off by a specific key on the keyboard (using pynput). I put the keyboard listen loop on the first thread and the action loop on the second thread. The problem is that after running the code, it does not immediately listen to the keyboard, only after 9-10 seconds. And sometimes it refuses to respond to the Esc button, and sometimes it works. How to get rid of delay?
from threading import Thread
from pynput import keyboard
import time
flag = False
kill = False
def on_press(key):
global flag
global kill
if key == keyboard.KeyCode.from_char('a'):
print('pressed A')
flag = not flag
if key == keyboard.Key.esc:
kill = True
return False
def thr2():
print('joining...')
with keyboard.Listener(on_press=on_press) as listen:
listen.join()
def thr1():
while True:
if kill:
break
if flag:
print('looping....')
time.sleep(0.4)
if __name__ == "__main__":
thread1 = Thread(target=thr1)
thread2 = Thread(target=thr2)
thread1.start()
thread2.start()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question