Python – Joining the Threads

Introduction

In multithreading, joining threads is an essential operation that ensures one thread waits for another to complete before proceeding. The join() method of a thread allows you to block the calling thread until the thread whose join() method is called terminates. This is useful for coordinating tasks and ensuring that resources are properly managed.

Using join() Method

The join() method is called on a thread instance. It makes the calling thread wait until the thread on which join() was called has finished executing.

Example

import threading
import time

def print_numbers():
    for i in range(1, 6):
        print(i)
        time.sleep(1)

# Create a thread
thread = threading.Thread(target=print_numbers)

# Start the thread
thread.start()

# Wait for the thread to complete
thread.join()

print("Thread execution completed.")

Output

1
2
3
4
5
Thread execution completed.

Joining Multiple Threads

You can create and join multiple threads to ensure that the main thread waits for all the threads to complete before proceeding.

Example

import threading
import time

def print_numbers(thread_id, n):
    for i in range(1, n + 1):
        print(f"Thread {thread_id}: {i}")
        time.sleep(1)

# Create multiple threads
threads = []
for i in range(3):
    thread = threading.Thread(target=print_numbers, args=(i + 1, 5))
    threads.append(thread)
    thread.start()

# Wait for all threads to complete
for thread in threads:
    thread.join()

print("All threads have finished execution.")

Output

Thread 1: 1
Thread 2: 1
Thread 3: 1
Thread 1: 2
Thread 2: 2
Thread 3: 2
Thread 1: 3
Thread 2: 3
Thread 3: 3
Thread 1: 4
Thread 2: 4
Thread 3: 4
Thread 1: 5
Thread 2: 5
Thread 3: 5
All threads have finished execution.

Timeout in join()

The join() method also supports a timeout parameter that specifies the maximum number of seconds to wait for the thread to complete. If the thread is still running after the timeout, the join() method returns, and the calling thread continues executing.

Example

import threading
import time

def long_running_task():
    print("Thread started")
    time.sleep(5)
    print("Thread finished")

# Create a thread
thread = threading.Thread(target=long_running_task)

# Start the thread
thread.start()

# Wait for the thread to complete with a timeout
thread.join(timeout=2)

if thread.is_alive():
    print("Thread is still running after timeout.")
else:
    print("Thread has completed.")

print("Main thread continues execution.")

Output

Thread started
Thread is still running after timeout.
Main thread continues execution.
Thread finished

Practical Example: Downloading Files Concurrently

Example

import threading
import time

def download_file(file_id):
    print(f"Starting download for file {file_id}")
    time.sleep(2)  # Simulate a file download with sleep
    print(f"Completed download for file {file_id}")

# Create threads for downloading multiple files
threads = []
for i in range(5):
    thread = threading.Thread(target=download_file, args=(i,))
    threads.append(thread)
    thread.start()

# Wait for all downloads to complete
for thread in threads:
    thread.join()

print("All file downloads have completed.")

Output

Starting download for file 0
Starting download for file 1
Starting download for file 2
Starting download for file 3
Starting download for file 4
Completed download for file 0
Completed download for file 1
Completed download for file 2
Completed download for file 3
Completed download for file 4
All file downloads have completed.

Conclusion

Joining threads in Python using the join() method ensures that one thread waits for another to complete, which is crucial for synchronizing tasks and managing resources effectively. You can join single or multiple threads and use a timeout to limit the waiting period. Understanding how to use the join() method is essential for building reliable and efficient multithreaded applications in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top