I am trying to run a thread in while loop but I am getting the error RuntimeError: threads can only be started once
kindly suggest a way to fix this
import sys
import threading
import time
import keyboard
def hd():
time.sleep(1)
print("hello")
cc = threading.Thread(target=hd)
while True:
time.sleep(3)
if keyboard.is_pressed("q"):
print("Q is pressed")
sys.exit()
else:
if not cc.is_alive():
cc.start()
CodePudding user response:
Check this post out: link. It provides some good insight into your question.
Threads are basically destroyed after they have completed. Editing your else statement to something like this might accomplish the task at hand:
if not cc.is_alive():
cc = threading.Thread(target=hd)
cc.start()