Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run multiple functions at same time [closed]

Tags:

python

I have the following code

my_func1()
my_func2()
my_func3()
my_func4()
my_func5()

Is it possible to calculate the data for the functions at the same time, instead of one after another?

like image 530
PiccolMan Avatar asked Sep 04 '15 18:09

PiccolMan


2 Answers

My favorite way is using concurrent.futures which is a standard Python library (version 3.2 and above or available as a separate package for Python 2.7):

from concurrent.futures import ThreadPoolExecutor

executors_list = []

with ThreadPoolExecutor(max_workers=5) as executor:
    executors_list.append(executor.submit(my_func1, arg1, arg2))
    executors_list.append(executor.submit(my_func2, arg1, arg2))
    executors_list.append(executor.submit(my_func3, arg1, arg2))

for x in executors_list:
    print(x.result())

This will run concurrently my_func1, my_func2 and my_func3, passing arg1 and arg2 to each one. It will then print sequentially all the results as soon as they're available.

like image 81
Giancarlo Sportelli Avatar answered Oct 24 '22 08:10

Giancarlo Sportelli


you can use multiprocessing or threading threading in python doesnt actually run in parallel but allow you to run the functions at the same time (and python will iterate them, doing a few lines from each at a time)

with multiprocessing they WILL run in parallel (assuming you have multiple cpu cores) but they wont share memory. here is a sample with multiprocessing

from multiprocessing import Process
p = Process(target=myfunc1)
p.start()
p2 = Process(target=myfunc2)
p2.start()
# and so on
p.join()
p2.join()
# the join means wait untill it finished

you can read more about it here:

https://docs.python.org/2/library/multiprocessing.html

https://wiki.python.org/moin/GlobalInterpreterLock

like image 34
DorElias Avatar answered Oct 24 '22 07:10

DorElias