I improved my execution time by using multiprocessing but I am not sure whether the behavior of the PC is correct, it freezes the system until all processes are done. I am using Windows 7 and Python 2.7.
Perhaps I am doing a mistake, here is what I did:
def do_big_calculation(sub_list, b, c):
# do some calculations here with the sub_list
if __name__ == '__main__':
list = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
jobs = []
for sub_l in list :
j = multiprocessing.Process(target=do_big_calculation, args=(sub_l, b, c))
jobs.append(j)
for j in jobs:
j.start()
Here, you are creating 1
Process
per task. This is will run all your tasks in parallel but it imposes an heavy overhead on your computer as your scheduler will need to manage many processes. This can cause a system freeze as too many ressources are used for your program.A solution here could be to use the
multiprocessing.Pool
to run a given number of processes simultaneously performing some tasks:If you are ready to use third party library, you could also take a look at
concurrent.futures
(you need to install it in python2.7 but it exists for python3.4+) orjoblib
(available with pip):The main advantage of such library is that it is developing whereas
multiprocessing
in python2.7 is freezed. Thus, there are bug fixes and improvements relatively often.It also implements some clever tools to reduce the overhead for the computation. For instance, it uses memory mapping for big numpy array (reducing the memory footprint of starting all jobs).