• Uncategorized

About python : Is-main-thread-same-as-parent-thread

Question Detail

I read singnal docs of python. It said singale handler must be registered in main thread.

If I start a thread (grand father) which start a child thread (father) which start another thread (grandson)

Does the father thread a main thread compared to grandson?

Question Answer

No. There is only one main thread. It is the thread which is running at program startup.

You can check which thread your code is running on with threading.current_thread() == threading.main_thread().

The signal.signal() documentation states:

When threads are enabled, this function can only be called from the main thread of the main interpreter; attempting to call it from other threads will cause a ValueError exception to be raised.

So, the best way to use this function would be to set up your signal handler before starting any threads.

Note that the documentation also says:

Python signal handlers are always executed in the main Python thread of the main interpreter, even if the signal was received in another thread. This means that signals can’t be used as a means of inter-thread communication.

If i understand the question correctly;
No, the grandson is not tracked by the mainthread inheritly. Although there are different ways you can start threads, one that is dependent on that the child thread is finished and closed before closing the parent or the parent continues in parallell.

But stating in the general case, the father is “owned” by the main thread while the child is “owned” by the father.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.