A Python thread is like a process, and may even be a process,
depending on the Python thread system. In fact, Python threads are sometimes called "lightweight" processes,
because threads occupy much less memory, and take less time to create, than do processes.
Threads allow applications to perform multiple tasks at once. Multi-threading is important in many applications.
Two modules, thread.py and threading.py serve as Python Threads.
We start our discussion from thread.py.
Python Threading modules
Define a new subclass of the Python Thread class.
New class inherit the Python threading class.
Override the constructor __init__(self [,args])
method to add additional arguments.
Then, override the run(self [,args])
method to implement what the thread should do when started
Learn by example
The above program gives the idea of minimum condition to run the Python threading program.
The run() method is the entry point for a python thread. This is the section where you can put your logic part
The start() method starts a Python thread by calling the run method
thread1 = mythread(1)
here subclass mythread
overrides the constructor
so base class constructor constructor (Thread.__init__())
must be invoked.
Let us discuss some more interesting program in python
Returns the number of total Python thread that are active.
Returns the number of Python thread objects in the caller's thread control.
Returns a list of all Python thread objects that are currently active.
Let us discuss our next example.
return 11 threads, 10 threads created by for
loop and one is main thread.
or sometime Join()
In very simple words Join()
function is used to terminate the threads.
Learn form example With-out join()
So you can see where is "good by". so my aim string "good by" should be print end of the program.
Now use I use join()
I think you understood what I want to show. Before print "good by" both threads are terminated.
Let us discuss another problem of join()
In above program you can see that threads are being created by for
loop and join()
is used within the for loop.
Consider you want to start all thread at once and work one by one ,So I use lock mechanism
Locks are the most fundamental synchronization mechanism provided by threading module.
A lock is in one of two states, locked or unlocked. If a thread attempts to hold a lock that's already held by
some other thread, execution of the first thread is halted until the lock is released.
Acquire a lock, blocking or non-blocking. When invoked with the blocking argument set to True (the default),
block until the lock is unlocked, then set it to locked and return True
When invoked with the blocking argument set to False, do not block.
Returns True if lock is locked, otherwise False.
Unlocks lock, which must be locked. When lock is locked, any thread may call lock
RLock is a reentrant lock.acquire()
can be called multiple times by the same thread without blocking. Keep in mind that release()
needs to be called the same number of times to unlock the resource.
Defined like this rlock = threading.RLock()
Let us discuss next example of threads with list.
In above program threads are added in the given list. In next for loop thread.join()
terminate the threads.
Python Daemon thread
Python docs says A thread can be flagged as a "daemon thread".
The significance of this flag is that the entire Python program exits when only daemon threads are left.
This must be set before start() is called, otherwise RuntimeError is raised.
Its initial value is inherited from the creating thread;
the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
The entire Python program exits when no alive non-daemon threads are left.
What is main thread ?
Let us learn by program
I have not declared any thread, why is the thread running ? Answer is,
a thread is like a process that one thing currently happening here
and your python program code is currently running with a thread.
Program you running a thread itself that is called main thread.
So I create another thread program.
return a list of active threads.
Check main thread is daemon or not.
The threading.enumerate() return the list of active threads and threading.enumerate() return first thread of list.
return False means main thread is not daemon thread. Thread created with in main thread is not daemon by default.
How to set particular threads is daemon.
use this syntax.
As shown in next program.
You can see thread2 is set as daemon thread.
If you want to only function and no class.
Just define the function and create thread like this
from threading import Thread
t = Thread(target=function_name, args=(i,))
= give the function name.
= argument pass to target function.
Now you can see in the program threads t
executes the function sleeper()
and threads t1
executes the function fun().
If you want to check the thread is alive or not at any moment , use isAlive()
Let us discuss in the program.
Output of the program is showing that t1 thread was not live and t2 was live before end of the program.
means thread is sleeping for 2 seconds and t1.join(3)
terminates the thread after
3 seconds so after 3 second thread t1 will die. For thread t2 terminate time is 1 second as mentioned in t2.join(1)
but during that time thread t2 was sleeping so thread was still alive.
Now back to Daemon thread definition
See the example below.
You can see the above program.
Two function n()
The daemon thread calls the function d()
and the non-daemon thread calls function n()
The daemon thread waits for the five seconds by using statement time.sleep(5)
but main thread does not wait for the daemon thread to exit. So statement "daemon stop" is not printed.
Now check the another example.
This time the non-daemon thread is waiting for 5 seconds, but main program waits for the non-daemon thread to exit.
Let us discuss our next example.
In above program main program waits for 5 second to exit.
In our above program, join() blocks the calling thread (main thread)
until the threads (d and t) whose join()
method is called is terminated.