У меня есть многопоточная программа Python, как показано ниже. Если я нажму ctrl + c в течение 5 секунд (приблизительно), он войдет в Исключение KeyboardInterrupt.Выход потоков Python с ctrl-c в Python
Выполнение кода более 15 секунд не отвечает на ctrl + c. Если я нажимаю ctrl + c через 15 секунд, он не работает. Это не бросает исключение KeyboardInterrupt. Что может быть причиной ? Я тестировал это на Linux.
#!/usr/bin/python
import os, sys, threading, time
class Worker(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# A flag to notify the thread that it should finish up and exit
self.kill_received = False
def run(self):
while not self.kill_received:
self.do_something()
def do_something(self):
[i*i for i in range(10000)]
time.sleep(1)
def main(args):
threads = []
for i in range(10):
t = Worker()
threads.append(t)
t.start()
while len(threads) > 0:
try:
# Join all threads using a timeout so it doesn't block
# Filter out threads which have been joined or are None
threads = [t.join(1) for t in threads if t is not None and t.isAlive()]
except KeyboardInterrupt:
print "Ctrl-c received! Sending kill to threads..."
for t in threads:
t.kill_received = True
if __name__ == '__main__':
main(sys.argv)