I don't manage to understand why my SIGINT is never catched by the piece of code below.
#!/usr/bin/env python
from threading import Thread
from time import sleep
import signal
class MyThread(Thread):
def __init__(self):
Thread.__init__(self)
self.running = True
def stop(self):
self.running = False
def run(self):
while self.running:
for i in range(500):
col = i**i
print col
sleep(0.01)
global threads
threads = []
for w in range(150):
threads.append(MyThread())
def stop(s, f):
for t in threads:
t.stop()
signal.signal(signal.SIGINT, stop)
for t in threads:
t.start()
for t in threads:
t.join()
To clean this code I would prefer try/except the join() and closing all threads in case of exception, would that work?
One of the problems with multithreading in python is that
join()
more or less disables signals.This is because the signal can only be delivered to the main thread, but the main thread is already busy with performing the
join()
and the join is not interruptible.You can deduce this from the documentation of the
signal
moduleYou can work your way around it, by busy-looping over the join operation:
This is, however, none to efficient:
Some more details are provided here:
Python program with thread can't catch CTRL+C
Bug reports for this problem with a discussion of the underlying issue can be found here:
https://bugs.python.org/issue1167930
https://bugs.python.org/issue1171023