Home >Backend Development >Python Tutorial >How Can I Run Multiple Python Functions Simultaneously Using Parallelism?

How Can I Run Multiple Python Functions Simultaneously Using Parallelism?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-18 05:06:02312browse

How Can I Run Multiple Python Functions Simultaneously Using Parallelism?

How to Execute Functions Simultaneously with Python Parallelism

In Python, seeking to execute multiple functions concurrently can be a challenge. In particular, when the functions operate independently without mutual interference, waiting for one function to complete before initiating another limits efficiency. To address this issue, Python offers two approaches: threading and multiprocessing.

Exploring Multiprocessing for True Parallelism

Multiprocessing is a preferable choice for achieving genuine parallelism in Python due to the constraints imposed by CPython. Here's a thorough example that demonstrates the use of multiprocessing:

from multiprocessing import Process

def func1():
    print("func1: starting")
    for i in range(10000000):
        pass
    print("func1: finishing")


def func2():
    print("func2: starting")
    for i in range(10000000):
        pass
    print("func2: finishing")


if __name__ == "__main__":
    p1 = Process(target=func1)
    p1.start()
    p2 = Process(target=func2)
    p2.start()
    p1.join()
    p2.join()

This approach employs the Process class from Python's multiprocessing module to spawn and manage child processes for each function. By starting and joining the processes, both func1 and func2 can execute in parallel.

Encapsulating Parallel Execution

The mechanics of starting and joining processes can be conveniently encapsulated into a function:

def runInParallel(*fns):
    proc = []
    for fn in fns:
        p = Process(target=fn)
        p.start()
        proc.append(p)
    for p in proc:
        p.join()

runInParallel(func1, func2)

This function takes any number of functions as input and initiates their parallel execution. It abstracts away the underlying process handling, allowing you to seamlessly execute multiple functions concurrently.

The above is the detailed content of How Can I Run Multiple Python Functions Simultaneously Using Parallelism?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn