Free cookie consent management tool by TermsFeed Python Thread - Constructor | Pythontic.com

Python Thread - Constructor

Method name:

Thread Constructor

Method signature:

thread(group = threadGroup, target = callable, name = threadName, args = data_to_thread, kwargs = data_to_thread_as_dict, daekon = isDaemon)

Parameters:

group: The threadGroup the thread belongs to. The threadGroup is a reserved parameter now. This will be used in the later implementations of Python when a ThreadGroup is implemented.
target: 
A function or any callable object that implements the body of the thread. When a function is used the name of the function to be given as the target.
name: Name of the thread. If provided
<name-decimal number> is the name of the thread. For example, WorkerThread-1. If not given it has a decimal value assigned to it by the implementation.        
args: It is a tuple. The reason it exists is to initialise the child thread with some data when it is created inside the main thread. Any complex multithreaded program will often use this future to pass some context from the main thread to the child thread. In some cases, later as threads make progress in the execution, the state of this data will be used by other threads to make synchronisation decisions (whether to resume themselves, acquire more resources etc).
kwargs: This parameter serves the same purpose as the previous parameter. It helps in initialising the child thread with data. This parameter is a dictionary of keyword arguments.
daemon: This parameter is to mention whether it is a daemon thread. Daemon threads continue to exist even after the main thread exits. Daemon threads are very useful in certain applications. e.g., an SSH database replication thread runs behind replicating every data that enters the Database. However, the program(the main thread) just starts and exits, leaving the replication thread to run forever.

Method overview:

Constructs a Thread object in Python

Example:

from threading import Thread

 

#A function that constitutes as the thread body in python

def ThreadFunction(TupleData, KeywordDictionary):

    print("Child Thread Started")

   

    for item in TupleData:

        print(item)

 

    for key in KeywordDictionary:

        print(key+":"+KeywordDictionary[key])

 

    print("Child Thread Exiting")       

 

 

print("Main Thread Started")

#Thread initialisation data as a python tuple

ThreadData1         = ("State1", "State2", "State3")  

 

#Thread initialisation data as a python dictionary

ThreadData2         = dict([("Key1", "Value1"), ("Key2", "Value2"), ("Key3", "Value3")])

 

#Construct a python thread object and initialise with data

SamplePythonThread  = Thread(target=ThreadFunction(ThreadData1,ThreadData2),name="ChildThread")

 

#Print the name of the child thread

print("Name of the Child Thread:"+SamplePythonThread.name)

 

#Start the child thread

SamplePythonThread.start()

 

#Let the main thread wait for the child thread to complete

SamplePythonThread.join()

 

print("Main Thread Exiting")

 Output:

I am the Daemon thread. I keep on running...hehe

My Daemon will take care


Copyright 2025 © pythontic.com