With “args” vs without “args” to pass arguments to a thread in Python

This does not call test in a new thread:

thread = threading.Thread(target=test("Test"))
thread.start()

Here’s how Python interprets those lines of code:

  1. Main thread calls test("Test").
  2. test("Test") returns None.
  3. Main thread calls Thread(target=None).*
  4. Main thread starts the new thread.
  5. New thread does absolutely nothing at all because its target is None.

Edit:

*I wondered why Thread(targe=None) does not raise an exception, but @Ahmed AEK explained it in a comment, below.

Leave a Comment