user1301404
user1301404

Reputation:

Python - Killing more than one thread on Control+C

What am I doing wrong ?

I simply need to kill both threads on Control+C.

def cleanup_stop_thread():
    for thread in enumerate():
        if thread.isAlive():
            try:
                self._Thread__stop()
            except:
                print(str(thread.getName()) + ' could not be terminated')

if __name__ == '__main__':  
    try:
        threading.Thread(target = record).start()
        threading.Thread(target = ftp).start()
    except (KeyboardInterrupt, SystemExit):
        cleanup_stop_thread();
        sys.exit()

Upvotes: 1

Views: 1734

Answers (1)

nneonneo
nneonneo

Reputation: 179717

Instead of trying to kill them on Ctrl+C, why don't you just make them daemon threads? Then they exit automatically when the main thread dies.

t1 = threading.Thread(target=record)
t1.daemon = True
t1.start()

t2 = threading.Thread(target=ftp)
t2.daemon = True
t2.start()

Upvotes: 6

Related Questions