Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Arguments of Thread Variable

I have a very simple question:

myThread = Thread(target=TestTarget, args=(1, Event(),))

Is it possible to get the arguments just using the Variable myThread?

Thank you!

like image 238
MisterPresident Avatar asked Mar 02 '26 13:03

MisterPresident


2 Answers

_Thread__args and _Thread__kwargs store the constructor's arguments.

However, as you might guess by the underscores, these are not part of the public API. Indeed, "mangled, renamed attributes" are intended to discourage direct access.

Additionally, these attributes are specific to the CPython implementation. Jython, for example, appears not to expose these attributes by those names (disclaimer: I did not test, instead just glanced at the source).

In your case, it would perhaps be better to store the arguments in some application-meaningful way in a subclass of Thread, and access those.

like image 60
pilcrow Avatar answered Mar 05 '26 03:03

pilcrow


You may simply use _Thread__arg on a Thread object to get the details of the arguments passed to that Thread object.

import threading

def TestTarget(a, b):
    pass

myThread = threading.Thread(target=TestTarget, args=(1, 2,))

print myThread._Thread__arg

>>> (1, 2)
like image 28
ZdaR Avatar answered Mar 05 '26 03:03

ZdaR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!