Python 3 Multithreading Example to Execute Function Code in Parallel & Concurrent Threads
163 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-multithreading-example-to-execute-function-code-in-parallel-concurrent-threads/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
package by which you can implement
0:06
multi-threading or parallel execution of
0:09
multiple functions at a single time so
0:12
we have basically prepared this Python
0:14
script which actually contains multiple
0:17
threads
0:19
and at concurrently it executes multiple
0:23
functions so let me just show you as I
0:25
run this Python script you will
0:28
see it is basically executing multiple
0:32
functions at the same time using uh the
0:35
concept of threading so threading as you
0:38
know that you can see both threads have
0:40
been finished executing
0:43
uh some programming language supports
0:45
multi-threading somes don't but in
0:47
Python as you all know it contains it's
0:50
a multi-threaded language it supports
0:52
multi-threading and JavaScript is a
0:54
single-threaded language c++ also
0:57
supports multi-threading java also
0:59
supports multi-threading so similarly in
1:02
Python we actually use this module which
1:06
is
1:07
threading so this is actually I think
1:10
it's a third party module
1:13
so it makes it easy to implement so just
1:17
search for this module which is
1:22
threading yeah so I think yeah this is
1:25
actually the
1:26
module by
1:28
default you install this pip install
1:31
threading 2 but you import this so I
1:35
will show you the complete example the
1:37
link is given in the description
1:40
so it's a very simple example so first
1:43
of all you simply import the
1:47
module the time module which is a
1:49
built-in module and then from this
1:51
threading
1:53
module I think you don't need to install
1:55
this it's built in inside the latest
1:58
version of Python which is 3
2:01
uh the Python 3 it's built in the
2:05
multi-threading support so you simply
2:07
import this module from threading import
2:09
the thread class after that you simply
2:13
define we define two functions first
2:16
function which
2:17
actually expects a argument to be passed
2:21
here which is a countdown function and
2:24
now inside this while loop we
2:27
simply countdown to those number we
2:30
simply print
2:33
out so this function actually accepts an
2:37
integer to be passed to this function to
2:40
actually render out the countdown string
2:43
so very simple we have defined this
2:45
while loop we wait for 1 second and the
2:48
second function which will be
2:50
responsible again here we'll be
2:52
providing a simple print message actual
2:56
message plus the count variable and then
2:59
we loop through and display these are
3:01
two different functions and now I will
3:04
show you the concept of threading so how
3:07
we do this we
3:10
actually open a new thread by using this
3:13
thread class and we specify the target
3:17
what target code that you want to
3:18
execute so here I will display this
3:21
countdown function that we declared and
3:24
then we can provide the arguments right
3:26
here which is the
3:29
five like this similarly we'll be
3:32
defining the second thread again by
3:34
using the thread class and this time
3:36
we'll be executing the second function
3:38
that we defined which is print
3:40
messages and the arguments that we need
3:43
to pass here the first will be the
3:46
string value hello from thread two and
3:51
then
3:52
five so these are two different threads
3:56
calling two different functions so these
3:58
are the first thread this is a second
4:00
thread calling the second
4:03
function so after that now to start the
4:06
thread we call the start function so t1
4:10
start t2 start so both the threads are
4:14
starting one after the other and
4:17
uh optionally we can also call the join
4:20
function as well so that we get the
4:23
message whenever both the threads have
4:25
been finished so after that we can
4:28
simply print a line both threads have
4:30
finished so now what happen if I execute
4:33
this Python
4:35
script both the threads will start
4:38
concurrently and as you can
4:41
see multi-threading is implemented in
4:44
this format so we have taken a very
4:45
simple example you can see we executed
4:49
both the threads at the same time
4:50
concurrently and then we simply print
4:53
out that both threads have finished so
4:55
in this easy way guys you can get
4:58
started using multi-threading inside
5:00
Python using this threading
5:03
module you define whatever code that you
5:06
need to just assign them into different
5:09
functions and then define different
5:11
threads uh thank you very much for
5:14
watching this video and also check out
5:16
my website freemediattools.com
5:19
uh which contains thousands of tools
#Computers & Electronics
#Programming
#Scripting Languages