4

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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, just once, as long as main terminates as a result of executing func.
you say that .daemon == True threads will be interrupted and killed when the main thread terminates?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.