I am using Python Threading where the attribute Daemon is set to true. I want to trigger the flag in my thread and if that thread is triggered, I want to exit the program. In my main loop I am getting the inpput
import threading
import time
import sys
class Test:
def __init__(self):
self.done = False
self.inp = ''
def worker(self):
count = 0
while True:
if self.inp == 'start':
print(f"Hello from Worker {count}")
count += 1
time.sleep(1)
if count == 3:
self.done = True
return
def start(self):
while not self.done:
self.inp = input('Enter input: ')
print(self.done)
time.sleep(1)
print("Main Thread ending")
sys.exit()
test = Test()
# Create a thread
thread = threading.Thread(target=test.worker)
# Set the thread as a daemon thread
thread.daemon = True
# Start the thread
thread.start()
test.start()
# Main thread sleeps for 5 seconds
time.sleep(5)
The main thread does not exit when I am getting the keyboard input, if I delete that code snippet, it exists gracefully.
New contributor
henryyy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.