Skip to content

Parallel Execution in Python Running Multiple Functions Threading

Parallel Execution in Python: Running Multiple Functions Threading
Share

Reading Time: 2 minutes

In this tutorial “Parallel Execution in Python Running Multiple Functions Threading”, We will explain how to run more than one function together.

What is Parallel Execution

You can run more than one functions , command etc together, It means you do not have to wait to get your first code to get finished hence both functions can run together. Threading allows you to run multiple functions concurrently, so you don’t have to wait for one function to finish before starting another.Parallel execution in Python with threading allows simultaneous operation of multiple functions, enhancing efficiency by eliminating wait times. This tutorial demonstrates how to employ threading to run distinct functions concurrently, fostering parallelism and optimizing code execution in Python.

Create the first function

In this code, we have created a function named ping_ntp_server and we are pinging google.com

def ping_ntp_server():
    hostname = "google.com" #example
    response = os.system("ping -c 3 " + hostname)

Create the second function

In this code we have created a function named “function_two” and simply printed message “This is function two”

def function_two():
    print('This is function two:')

This way, both ping_ntp_server and function_two can run concurrently. It’s important to note that threading in Python might not provide true parallelism due to the Global Interpreter Lock (GIL), which ensures only one thread executes Python bytecode at a time. For CPU-bound tasks, you may want to consider the multiprocessing module for parallel execution.

Additionally, there’s a small typo in the code where the second function is named function_two, but the print statement inside it mentions “This is Second function:”. You might want to make the names consistent for clarity.

This code is a basic example of using threading for parallel execution. Depending on your specific use case, you may need to consider synchronization mechanisms and potential issues related to shared resources in a multithreaded environment.

Create a thread in Python

You need to initialize Thread and use your both function then you need to start thread.

Note: You can start thread once.

thread_one = threading.Thread(target=ping_ntp_server)
thread_two = threading.Thread(target=function_two)

Start both thread

thread_one.start()
thread_two.start()

Parallel Execution in Python Running Multiple Functions Threading

Code

import threading, os

def ping_ntp_server():
    hostname = "google.com" #example
    response = os.system("ping -c 3 " + hostname)
    
def function_two():
    print('This is Second function:')
    

thread_one = threading.Thread(target=ping_ntp_server)
thread_two = threading.Thread(target=function_two)

thread_one.start()
thread_two.start()
Parallel Execution in Python: Running Multiple Functions Threading

What is parallel execution in Python, and how can it be achieved using threading?

Parallel execution in Python involves running multiple functions concurrently to improve efficiency. Using threading, you can achieve this by creating separate threads for each function and starting them simultaneously, as demonstrated in the provided code snippet

Conclusion

We hope this tutorial “Parallel Execution in Python Running Multiple Functions Threading” explains to you the concept of Parallel Execution in Python and how to use threading.
You can find all “Linux Tutorial”


Share

Leave a Reply

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