Multithreading in Python - GeeksforGeeks
Multithreading in Python - GeeksforGeeks
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.
An executable program.
The associated data needed by the program
(variables, workspace, buffers, etc.)
The execution context of the program (State
of the process)
Multithreading in Python
import threading
t1 = threading.Thread(target, args)
t2 = threading.Thread(target, args)
t1.start()
t2.start()
Step 4: End the thread Execution
t1.join()
t2.join()
Example:
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!
Multithreading
Example:
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__":
t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')
t1.start()
t2.start()
t1.join()
t2.join()
Output:
Multithreading
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.
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)
Output
Previous Next
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
Complete Tutorials
Python API
Python Crash
Tutorial: Getting
Course
Started with APIs
Python
Advanced Python
Automation
Tutorials
Tutorial
GeeksforGeeks
Additional Information
Learn More
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
NLP Tutorial
Deep
Learning
Tutorial
Interview
Questions
Preparation School Management Free More GeeksforGeeks
Corner Subjects & Finance Online Tutorials Videos
Random
Password
Generator