Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
109 views

Multithreading in Python - GeeksforGeeks

Uploaded by

walteravelin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Multithreading in Python - GeeksforGeeks

Uploaded by

walteravelin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Tutorials DSA Data Science Web Tech Courses Sign In

Multithreading in Python
This article covers the basics of multithreading in
Python programming language. Just like
multiprocessing, multithreading is a way of
achieving multitasking. In multithreading, the
concept of threads is used. Let us first
understand the concept of thread in computer
architecture.

What is a Process in Python?


In computing, a process is an instance of a
computer program that is being executed. Any
process has 3 basic components:

An executable program.
The associated data needed by the program
(variables, workspace, buffers, etc.)
The execution context of the program (State
of the process)

An Intro to Python Threading


A thread is an entity within a process that can be
scheduled for execution. Also, it is the smallest
unit of processing that can be performed in an
OS (Operating System). In simple words, a
thread is a sequence of such instructions within a
program that can be executed independently of
other code. For simplicity, you can assume that a
thread is simply a subset of a process! A thread
contains all this information in a Thread Control
Block (TCB):
Thread Identifier: Unique id (TID) is assigned
to every new thread
Stack pointer: Points to the thread’s stack in
the process. The stack contains the local
variables under the thread’s scope.
Program counter: a register that stores the
address of the instruction currently being
executed by a thread.
Thread state: can be running, ready, waiting,
starting, or done.
Thread’s register set: registers assigned to
thread for computations.
Parent process Pointer: A pointer to the
Process control block (PCB) of the process
that the thread lives on.

Consider the diagram below to understand the


relationship between the process and its thread:
Relationship between a Process and its Thread

Multiple threads can exist within one process


where:

Each thread contains its own register set and


local variables (stored in the stack).
All threads of a process share global
variables (stored in heap) and the program
code.

Consider the diagram below to understand how


multiple threads exist in memory:
Existence of multiple threads in memory

An Intro to Threading in Python


Multithreading is defined as the ability of a
processor to execute multiple threads
concurrently. In a simple, single-core CPU, it is
achieved using frequent switching between
threads. This is termed context switching. In
context switching, the state of a thread is saved
and the state of another thread is loaded
whenever any interrupt (due to I/O or manually
set) takes place. Context switching takes place
so frequently that all the threads appear to be
running parallelly (this is termed multitasking).

Consider the diagram below in which a process


contains two active threads:
Multithreading

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 = threading.Thread(target, args)
t2 = threading.Thread(target, args)

Step 3: Start a Thread

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


the Thread class.

t1.start()
t2.start()
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.

t1.join()
t2.join()

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.

This code demonstrates how to use Python’s


threading module to calculate the square and
cube of a number concurrently. Two threads, t1
and t2, are created to perform these calculations.
They are started, and their results are printed in
parallel before the program prints “Done!” when
both threads have finished. Threading is used to
achieve parallelism and improve program
performance when dealing with computationally
intensive tasks.

Python3
import threading

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

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

if __name__ =="__main__":
t1 = threading.Thread(target=print_square, args=(10,))
t2 = threading.Thread(target=print_cube, args=(10,))

t1.start()
t2.start()

t1.join()
t2.join()

print("Done!")

Output:

Square: 100
Cube: 1000
Done!

Consider the diagram below for a better


understanding of how the above program works:

Multithreading
Example:

In this example, we use os.getpid() function to


get the ID of the current process. We use
threading.main_thread() function to get the
Free Python 3 Tutorial Data Types Control Flow Functions List String Set Tuple Dictionary Oops Exception Handling
main thread object. In normal conditions, the
main thread is the thread from which the Python
Solve Coding
Problems
interpreter was started. name attribute of the
thread object is used to get the name of the
range() vs xrange() in thread. Then we use the
Python
threading.current_thread() function to get the
current thread object.
Python Modules

Regular Expression Consider the Python program given below in


(RegEx) in Python with which we print the thread name and
Examples corresponding process for each task.

str() vs repr() in Python This code demonstrates how to use Python’s


threading module to run two tasks concurrently.
Logging in Python
The main program initiates two threads, t1 and
t2, each responsible for executing a specific task.
Using ipdb to Debug
Python Code The threads run in parallel, and the code
provides information about the process ID and
Generators in Python
thread names. The os module is used to access
the process ID, and the ‘threading' module is
Python Keywords
used to manage threads and their execution.
Switch Case in Python
(Replacement) Python3
Time Functions in import threading
Python | Set 1 (time(), import os
ctime(), sleep()...)
def task1():
print("Task 1 assigned to thread: {}".format(threading.current_thread().name)
print("ID of process running task 1: {}".format(os.getpid()))

def task2():
print("Task 2 assigned to thread: {}".format(threading.current_thread().name)
print("ID of process running task 2: {}".format(os.getpid()))

if __name__ == "__main__":

print("ID of process running main program: {}".format(os.getpid()))

print("Main thread name: {}".format(threading.current_thread().name))

t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')

t1.start()
t2.start()

t1.join()
t2.join()
Output:

ID of process running main program:


1141
Main thread name: MainThread
Task 1 assigned to thread: t1
ID of process running task 1: 1141
Task 2 assigned to thread: t2
ID of process running task 2: 1141

The diagram given below clears the above


concept:

Multithreading

So, this was a brief introduction to


multithreading in Python. The next article
in this series covers synchronization
between multiple threads. Multithreading
in Python | Set 2 (Synchronization)

Python ThreadPool
A thread pool is a collection of threads that are
created in advance and can be reused to execute
multiple tasks. The concurrent.futures module in
Python provides a ThreadPoolExecutor class that
makes it easy to create and manage a thread
pool.

In this example, we define a function worker that


will run in a thread. We create a
ThreadPoolExecutor with a maximum of 2
worker threads. We then submit two tasks to the
pool using the submit method. The pool
manages the execution of the tasks in its worker
threads. We use the shutdown method to wait
for all tasks to complete before the main thread
continues.

Multithreading can help you make your programs


more efficient and responsive. However, it’s
important to be careful when working with
threads to avoid issues such as race conditions
and deadlocks.

This code uses a thread pool created with


concurrent.futures.ThreadPoolExecutor to
run two worker tasks concurrently. The main
thread waits for the worker threads to finish
using pool.shutdown(wait=True). This allows
for efficient parallel processing of tasks in a
multi-threaded environment.

Python3

import concurrent.futures

def worker():
print("Worker thread running")

pool = concurrent.futures.ThreadPoolExecutor(max_workers=2)

pool.submit(worker)
pool.submit(worker)

pool.shutdown(wait=True)

print("Main thread continuing to run")

Output

Worker thread running


Worker thread running
Main thread continuing to run
Don't miss your chance to ride the wave of the
data revolution! Every industry is scaling new
heights by tapping into the power of data.
Sharpen your skills and become a part of the
hottest trend in the 21st century.

Dive into the future of technology - explore the


Complete Machine Learning and Data Science
Program by GeeksforGeeks and stay ahead of
the curve.

Last Updated : 09 Nov, 2023 197

Previous Next

Space complexity of Basic Slicing and


List Operations in Advanced Indexing
Python in NumPy

Share your thoughts in


Add Your Comment
the comments

Similar Reads
Di!erence Multithreading or
Between Multiprocessing
Multithreading vs with Python and
Multiprocessing in Selenium
Python
Multithreading in Important
Python | Set 2 di!erences
(Synchronization) between Python
2.x and Python 3.x
Python program to with examples
Python | Merge
build flashcard Python key values
using class in to list
Python
Reading Python Python | Add
File-Like Objects Logging to a
from C | Python Python Script

Python | Add JavaScript vs


Logging to Python Python : Can
Libraries Python Overtop
JavaScript by
2020?

Complete Tutorials
Python API
Python Crash
Tutorial: Getting
Course
Started with APIs

Python
Advanced Python
Automation
Tutorials
Tutorial

OpenAI Python API


- Complete Guide

GeeksforGeeks

Article Tags : Python , Technical Scripter

Practice Tags : python

Additional Information

Learn More

Company Explore Languages DSA Data HTML &

A-143, 9th Floor, Sovereign About Us Hack-A- Python Data Science & CSS
Corporate Tower, Sector-136, Thons Structures ML
Noida, Uttar Pradesh - Legal Java HTML
201305 GfG Weekly Algorithms
Careers C++ Data Science CSS
Contest DSA for With Python
In Media PHP Web
DSA in Beginners Data Science Templates
Contact Us GoLang
JAVA/C++
Contact Us GoLang
JAVA/C++ Basic DSA For Beginner
Advertise SQL CSS
Master Problems Machine Frameworks
with us R Language
System DSA Learning
GFG Bootstrap
Design Android Roadmap Tutorial
Corporate Tutorial Tailwind CSS
Master CP Top 100 DSA ML Maths
Solution SASS
GeeksforGeeks Tutorials Interview
Placement Data
Videos Archive Problems LESS
Training Visualisation
Geeks DSA Tutorial Web Design
Program
Community Roadmap by Pandas Django
Sandeep Jain Tutorial Tutorial

All Cheat NumPy


Sheets Tutorial

NLP Tutorial

Deep
Learning
Tutorial

Python Computer DevOps Competitive System JavaScript

Python Science Git Programming Design JavaScript


Programming Operating AWS Top DS or High Level Examples
Examples Systems Algo for CP Design TypeScript
Docker
Python Computer Top 50 Tree Low Level ReactJS
Kubernetes
Projects Network Design
Azure Top 50 Graph NextJS
Python Database UML
GCP Top 50 Array AngularJS
Tkinter Management Diagrams
DevOps Top 50 String NodeJS
Web Scraping System Interview
Roadmap Top 50 DP Lodash
OpenCV So!ware Guide
Python Engineering Top 15 Web Browser
Design
Tutorial Websites for
Digital Logic Patterns
CP
Python Design OOAD
Interview Engineering System
Question Maths Design
Bootcamp

Interview
Questions
Preparation School Management Free More GeeksforGeeks
Corner Subjects & Finance Online Tutorials Videos

Company- Mathematics Management Tools So!ware DSA


Wise Physics HR Typing Test Development Python
Recruitment Management So!ware
Chemistry Image Editor Java
Process Testing
Biology Finance Code C++
Resume Product
Social Income Tax Formatters Data Science
Templates Management
Science Organisational Code CS Subjects
Aptitude SAP
English Behaviour Converters
Preparation
Grammar Marketing Currency SEO - Search
Puzzles Engine
World GK Converter
Company- Optimization
Random
Wise Linux
Number
Preparation
Generator Excel

Random
Password
Generator

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

You might also like