Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing same Python program with different arguments in parallel

How can i execute the same python program in parallel (I am thinking of x10) with the only difference being two input arguments representing a time-range? I need it for some data-processing, which otherwise will take too long to finish

I know i can do it manually in shell by starting 10 scripts one by one, but it does not seem to be the most "elegant" solution + I would also love to define arguments for each of those programs dynamically in the "main" python program.

Is there a way to do it?

like image 716
Denys Avatar asked Jun 12 '16 12:06

Denys


People also ask

How do I run the same Python script in parallel?

We can also run the same function in parallel with different parameters using the Pool class. For parallel mapping, We have to first initialize multiprocessing. Pool() object. The first argument is the number of workers; if not given, that number will be equal to the number of elements in the system.

How do you call the same function with different parameters in Python?

Method overloading is a means by which you can call the same method in different ways, i.e. with different parameters based on the number of arguments or their different datatypes. Just like we did above, we call the same function, Mul with different number of parameters.

How do I run multiple Python files in parallel?

Yes, you can run multiple python scripts at once and In python, we use multi-threading to run multiple works simultaneously. The simplest solution to run two Python processes concurrently is to run them from a bash file, and tell each process to go into the background with the & shell operator.

How do you run a parallel function in Python?

One common way to run functions in parallel with Python is to use the multiprocessing module which is powerful, it has many options to configure and a lot of things to tweak.


1 Answers

Enclose your script in a main method, like so:

def main(args):
    a, b = args
    # do something


if __name__ == '__main__':
    args = parse_arguments()
    main(args)

Then you can use a second script together with multiprocessing.Pool, to run the main method with different arguments.

from myscript import main
from multiprocessing import Pool



a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]

if __name__ == '__main__':
    with Pool(4) as pool: # four parallel jobs
        results = pool.map(main, zip(a, b))
like image 141
MaxNoe Avatar answered Oct 27 '22 05:10

MaxNoe