Python – Creating a Thread

What is Python?

Python is a high-level, interpreted programming language that is known for its simplicity and readability. It was created by Guido van Rossum and first released in 1991. Python has gained popularity among developers due to its versatility and extensive libraries, making it suitable for a wide range of applications.

What are Threads in Python?

In Python, a thread is a separate flow of execution within a program. It allows a program to perform multiple tasks concurrently, improving efficiency and responsiveness. Threads are lightweight and share the same memory space, making communication between them easier.

Creating a Thread in Python

Python provides a built-in module called threading for creating and managing threads. Here’s an example of how to create a thread in Python:

import threading

def my_function():
    # Code to be executed in the thread

# Create a new thread
my_thread = threading.Thread(target=my_function)

# Start the thread
my_thread.start()

# Wait for the thread to finish
my_thread.join()

In the above example, we first import the threading module. Then, we define a function called my_function() which contains the code that will be executed in the thread. Next, we create a new thread using the Thread() constructor, passing the target function as an argument. Finally, we start the thread using the start() method and wait for it to finish using the join() method.

It’s important to note that the code inside the my_function() will run concurrently with the main program. This allows us to perform multiple tasks simultaneously, such as handling user input while performing a background task.

Example: Printing Numbers Using Threads

Let’s see a practical example of using threads in Python. In this example, we’ll create two threads that print numbers from 1 to 10:

import threading

def print_numbers():
    for i in range(1, 11):
        print(i)

# Create the first thread
thread1 = threading.Thread(target=print_numbers)

# Create the second thread
thread2 = threading.Thread(target=print_numbers)

# Start both threads
thread1.start()
thread2.start()

# Wait for both threads to finish
thread1.join()
thread2.join()

In the above example, we define a function called print_numbers() that uses a loop to print numbers from 1 to 10. We then create two threads, thread1 and thread2, and assign the print_numbers() function as the target for both threads. Finally, we start both threads and wait for them to finish.

When executing the above code, you may notice that the numbers are not printed in sequential order. This is because the two threads are running concurrently, and their execution order is non-deterministic. However, you’ll still see all the numbers from 1 to 10 printed.

Conclusion

Threads in Python allow for concurrent execution, enabling programs to perform multiple tasks simultaneously. By using the threading module, you can easily create and manage threads in your Python programs. Whether you’re handling background tasks, improving responsiveness, or implementing parallel processing, threads are a powerful tool in Python’s arsenal.

Scroll to Top