38

I have test() as shown below:

def test(arg1, arg2=None, arg3=None):

Now, I tries to create a thread using test(), and giving it only arg1 and arg2 but not arg3 as shown below:

threading.Thread(target=test, args=(arg1, arg2=arg2)).start()

But, I got a syntax error. How can I solve the error so that I can pass an argument to the thread as arg2?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Dylan
  • 949
  • 3
  • 13
  • 23

2 Answers2

73

Use the kwargs parameter:

threading.Thread(target=test, args=(arg1,), kwargs={'arg2':arg2}).start()
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
6

you can also use lambda to pass args

threading.Thread(target=lambda: test(arg1, arg2=arg2, arg3=arg3)).start()
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Ali80
  • 6,333
  • 2
  • 43
  • 33