0% found this document useful (0 votes)
4 views2 pages

Multi

The document outlines a step-by-step guide to using the threading module in Python to run tasks in parallel. It includes importing the module, defining tasks, creating thread objects, starting the threads, and waiting for their completion. An optional final message indicates when both tasks are completed.

Uploaded by

SHAHIDHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Multi

The document outlines a step-by-step guide to using the threading module in Python to run tasks in parallel. It includes importing the module, defining tasks, creating thread objects, starting the threads, and waiting for their completion. An optional final message indicates when both tasks are completed.

Uploaded by

SHAHIDHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Step 1: Import the threading module

import threading
This module helps create and manage threads.

Step 2: Define the functions (tasks) to run in threads


Each function will represent a separate task to run in
parallel.
def task1():
for i in range(5):
print("Task 1 running")

def task2():
for i in range(5):
print("Task 2 running")

Step 3: Create Thread objects


Use threading.Thread() to create a thread for each task.
t1 = threading.Thread(target=task1)
t2 = threading.Thread(target=task2)

Step 4: Start the threads


This tells Python to begin executing the functions in
parallel.
t1.start()
t2.start()

Step 5: Wait for threads to complete


Use join() to wait until each thread finishes its job.
t1.join()
t2.join()

Step 6: Final message (optional)


print("Both tasks completed.")

You might also like