Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 02

Function Threads

Start a Python child thread with threading.Thread, point it at a function, pass a tuple of arguments, and dispatch that function with start().

Advanced25 min
Function threads hero Module 19 Lesson 2 target and start

Start a Python child thread with threading.Thread, point it at a function, pass a tuple of arguments, and dispatch that function with start().

The function approach

Example

python
import threading

def announce(m):
    print(m, "on", threading.current_thread().name)

first = threading.Thread(target=announce, args=("Avery",), name="lane")
second = threading.Thread(target=announce, args=("Jordan",), name="hale")
first.start()
second.start()
first.join()
second.join()

Both names print. On a desktop Python, the order of the two lines can vary because the children run as separate flows. args is a tuple, so one value is written ("Avery",) with the comma. The name attribute replaces the older setName method. Inside announce, current_thread().name is the child's name. The playground uses the same target, and if start() is refused it calls announce on MainThread.

Key takeaways

  • threading.Thread builds the child; it does not run the function yet

  • target is the function object, and args is a tuple

  • start() dispatches the child

  • Read and write the name with the name attribute

  • current_thread() tells you which flow is inside the function

  • Next: Class Threads — pass a method as the target

  • Previous: Threads Overview

Loading playground…

Check your understanding

Function Threads Quiz

Which call actually begins the child thread's work?

1 / 10