I would like to run a daemon thread in a cell and be able via SIGINT (CTRL + C) to stop the thread from the same cell.
i.e.
import threading
import time
import sys
def number_generator():
num = 0
while True:
yield num
num += 1
gen = number_generator()
def daemon_task():
while True:
sys.stdout.write(f"\rDaemon working... {next(gen)}")
sys.stdout.flush()
time.sleep(1)
thread = threading.Thread(target=daemon_task, daemon=True)
thread.start()
When launching this code in a Jupyter cell, it’ll keep printing on the same line. I would like from the same cell to interrupt the execution. Is this possible at all?