views:

27

answers:

1

I have a multithreaded program and use the signal.signal(SIGINT,func) to kill all threads when ctrl c is pressed. The question I have is this:

I have to call signal.signal(...) from main in python. Do I have to call that on a loop or can I just set it once and whenever the user presses ctrl c, the signal will be caught?

+1  A: 

Only the main tread can handle signals. Just make all your threads "daemonic" ones (set the thread object's .daemon property to True before you start the thread) to ensure the threads terminate when the main thread does.

Alex Martelli
Ok, thanks, but in main, do I need to call signa.signal(SIGINT, func) in a loop (e.g. While true) or can I just call it once in main?
Juli
@Juli, just once, as long as `main` terminates as a result of executing `func`.
Alex Martelli