Код: Выделить всё
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)
Подробнее здесь: https://stackoverflow.com/questions/784 ... xit-the-ma