views:

32

answers:

1

Hello.

When using threading module and Thread() class, SIGINT (Ctrl+C in console) could not be catched.

Why and what can I do?

Simple test program:

#!/usr/bin/env python

import threading

def test(suffix):
    while True:
        print "test", suffix

def main():
    for i in (1, 2, 3, 4, 5):
        threading.Thread(target=test, args=(i, )).start()

if __name__ == "__main__":
    main()

Try hitting Ctrl-C - nothing happens.

+1  A: 

Threads and signals don't mix. In Python this is even more so the case than outside: signals only ever get delivered to one thread (the main thread); other threads won't get the message. There's nothing you can do to interrupt threads other than the main thread. They're out of your control.

The only thing you can do here is introduce a communication channel between the main thread and whatever threads you start, using the queue module. You can then send a message to the thread and have it terminate (or do whatever else you want) when it sees the message.

Alternatively, and it's often a very good alternative, is to not use threads. What to use instead depends greatly on what you're trying to achieve, however.

Thomas Wouters
Main thread don't receive SIGINT either. Otherwise I could just catch it and call sys.exit(0).
Marko Kevac
Seems that I am wrong. Main thread do receive SIGINT. Thank you!
Marko Kevac
But I don't understand why sys.exit(0) don't work in signal handler.
Marko Kevac
Python is waiting for the threads you started (with `threading.Thread`) to end. You can set the threads daemonic, which prevents that, but that will cause loud errors as the thread that's still running has the Python environment brutally ripped from under it. You have to ask the thread to exit, or not use threads.
Thomas Wouters