from threading import Thread
from time import sleep
def worker(i):
while True:
print("worker {}".format(i))
sleep(i)
threads = [Thread(target=worker, args=(i,), daemon=True) for i in range(1, 4)]
for t in threads:
t.start()
try:
for t in threads:
t.join()
except KeyboardInterrupt:
pass
from threading import Thread
from time import sleep
is_stopped = False
def worker(i):
global is_stopped
while not is_stopped:
try:
print("worker {}".format(i))
sleep(i)
except KeyboardInterrupt:
is_stopped = True
threads = [Thread(target=worker, args=(i,)) for i in range(1, 4)]
for t in threads:
t.start()
for t in threads:
t.join()