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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With