0% found this document useful (0 votes)
11 views4 pages

Python Multithreading Tutorial

The document provides an introduction to multithreading in Python using the threading module. It outlines the steps to create, start, and manage threads, including code examples for printing squares and cubes of a number. The example demonstrates how to execute multiple threads concurrently and wait for their completion before proceeding with the main program.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Python Multithreading Tutorial

The document provides an introduction to multithreading in Python using the threading module. It outlines the steps to create, start, and manage threads, including code examples for printing squares and cubes of a number. The example demonstrates how to execute multiple threads concurrently and wait for their completion before proceeding with the main program.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Multithreading in Python

An Intro to Threading in Python

Multithreading is defined as the ability of a processor to execute


multiple threads concurrently…

Multithreading in Python

In Python, the threading module provides a very simple and intuitive API for
spawning multiple threads in a program. Let us try to understand
multithreading code step-by-step.

Step 1: Import Module


First, import the threading module.
import threading
Step 2: Create a Thread

To create a new thread, we create an object of the Thread class. It takes the
‘target’ and ‘args’ as the parameters. The target is the function to be executed
by the thread whereas the args is the arguments to be passed to the target
function.
t1 = [Link](target, args)
t2 = [Link](target, args)

Step 3: Start a Thread

To start a thread, we use the start() method of the Thread class.


[Link]()
[Link]()

Step 4: End the thread Execution

Once the threads start, the current program (you can think of it like a main
thread) also keeps on executing. In order to stop the execution of the current
program until a thread is complete, we use the join() method.
[Link]()
[Link]()

As a result, the current program will first wait for the completion of t1 and
then t2. Once, they are finished, the remaining statements of the current
program are executed.

Example:
Let us consider a simple example using a threading module.

 Python3
# Python program to illustrate the concept

# of threading

# importing the threading module

import threading

def print_cube(num):

# function to print cube of given num

print("Cube: {}" .format(num * num * num))

def print_square(num):

# function to print square of given num

print("Square: {}" .format(num * num))

if __name__ =="__main__":

# creating thread

t1 = [Link](target=print_square, args=(10,))

t2 = [Link](target=print_cube, args=(10,))

# starting thread 1

[Link]()

# starting thread 2

[Link]()
# wait until thread 1 is completely executed

[Link]()

# wait until thread 2 is completely executed

[Link]()

# both threads completely executed

print("Done!")

Output:
Square: 100
Cube: 1000
Done!

You might also like