Python multiprocessing get return value

The Python multiprocessing module allows you to write efficient concurrent code by taking advantage of multiple CPUs or cores on a machine. One of the common use cases is to retrieve the return values from multiple processes.

Here is an example that demonstrates how to use multiprocessing to get return values from multiple processes:


    import multiprocessing

    def square(number):
        return number * number

    if __name__ == '__main__':
        numbers = [1, 2, 3, 4, 5]

        pool = multiprocessing.Pool()
        results = pool.map(square, numbers)
        pool.close()
        pool.join()

        print(results)
  

In the above example, we define a function named square that takes a number as an argument and returns its square. We create a list of numbers and then use a multiprocessing.Pool object to create a pool of worker processes.

We use the map method of the pool object to apply the square function to each number in the list. The map method divides the work among the available processes and returns a list of results.

After the map operation is complete, we close the pool to prevent any new tasks from being submitted, and then call the join method to wait for all the processes to finish.

Finally, we print the list of results. In this example, the output will be [1, 4, 9, 16, 25], which are the squares of the original numbers.

The pool.map() method is a blocking operation, meaning it will wait for all processes to finish before returning the results. If you need to get the results as soon as they are available, you can use the pool.imap() or pool.imap_unordered() methods instead.

With the pool.imap() method, you can iterate over the results as they are being computed, while the pool.imap_unordered() method allows you to retrieve the results in any order they become available, without waiting for the slowest task to finish.

Leave a comment