Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

threading - how to get parent id/name?

I looking for the way to get parent ID or name from child thread. In example, I have main thread as MainThread. In this thread i create a few new threads. Then I use threading.enumerate() to get references to all running thread, pick one of child threads and somehow get ID or name of MainThread. Is any way to do that?

like image 714
Galmi Avatar asked Jul 01 '11 18:07

Galmi


1 Answers

Make a Thread subclass that sets a parent attribute on init:

from threading import current_thread

class MyThread(threading.Thread):
    def __init__(self, *args, **kwargs):
        self.parent = current_thread()
        Thread.__init__(self, *args, **kwargs)

Then, while doing work inside a thread started with this class, we can access current_thread().parent to get the spawning Thread object.

like image 124
eternicode Avatar answered Sep 27 '22 22:09

eternicode