How to prevent Exception ignored in: <module 'threading' from ... > while setting signal handler? How to prevent Exception ignored in: <module 'threading' from ... > while setting signal handler? multithreading multithreading

How to prevent Exception ignored in: <module 'threading' from ... > while setting signal handler?


The problem is that you didn't set your threads to be daemons and you didn't join the threads, so when the main thread dies the rest keep running in the background.

If you edit your code to be as follows then it will work:

import signalimport threadingimport sysdef signal_handler(signal, frame):    print("exiting")    sys.exit(0)if __name__ == "__main__":    signal.signal(signal.SIGINT, signal_handler)    threads_arr = []    for i in list:        t = threading.Thread(target=myFunc, args=(i))        threads_arr.append(t)        t.daemon = True # die when the main thread dies        t.start()    for thr in threads_arr: # let them all start before joining        thr.join()