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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With