简体   繁体   中英

Python threading arguments passing

I would like to understand the difference between the ways we can pass arguments to threads and if this has any effect on thread safety.

I am using python 3.7.3 and both instances of the code runs fine for me.

Example 1:
thread = threading.Thread(target=MultiHandler().handle, args=(argument))

Example 2:
thread = threading.Thread(target=MultiHandler().handle(argument))

target should be the callable object to be invoked, not the result of the function call,
unless your 2nd sample function returns another callable ( target=MultiHandler().handle(argument) returns ---> callable ).

Python allows both args and kwargs in threading as arguments. And that can be used to make decisions inside a function. And it's the callable function where thread safety applies not on the arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM