Manoj Kumar M
Manoj Kumar M

Reputation: 165

Passing multiple arguments in Python thread

The following code passes a list (varbinds) and it works fine.

t1 = threading.Thread(target = Main2_TrapToTxtDb, args = (varBinds,))

Now I need to pass another variable - vString along with this.

Please help with a simple code.

Upvotes: 12

Views: 36775

Answers (1)

Right leg
Right leg

Reputation: 16730

The args parameter is a tuple, and allows you to pass many arguments to the target.

t1 = threading.Thread(target=Main2_TrapToTxtDb, args=(varBinds, otherVariable))

This is documented here:

threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

Upvotes: 16

Related Questions