I am working on a multithread traceback script, I am using the following code sample to retrieve the name of the thread, is there a nicer way to get the name of the thread out of the thread id?
for threadId, stack in sys._current_frames().items():
tname = "None"
for mthread in threading.enumerate():
if mthread.ident == threadId:
tname = mthread.name
Not in the public interface of threading
. Internally, threading
maintains exactly the mapping you want, so you could write (at your own risk)
def thread_for_ident(ident):
return threading._active.get(ident)
which will return None
if there is no such thread. I don't think you're solution is actually too bad as long as there aren't many threads.
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