multiprocessing freeze computer
<p>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.</p>

<p>Perhaps I am doing a mistake, here is what I did:</p>

<pre><code>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()
</code></pre>

<p>Here, you are creating 1 <code>Process</code> 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.  </p>

<p>A solution here could be to use the <code>multiprocessing.Pool</code> to run a given number of processes simultaneously performing some tasks:</p>

<pre><code>import multiprocessing as mp

def do_big_calculation(args):
    sub_list, b, c = args
    return 1

if __name__ == '__main__':
    b, c = 1, 1
    ll = [([1, 2, 3, 4], b, c),
          ([5, 6, 7, 8], b, c),
          ([9, 10, 11, 12], b, c)]
    pool = mp.Pool(4)
    result = pool.map(do_big_calculation, ll)
    pool.terminate()
    print(result)
</code></pre>

<p>If you are ready to use third party library, you could also take a look at <a href="https://github.com/agronholm/pythonfutures" rel="nofollow"><code>concurrent.futures</code></a> (you need to install it in python2.7 but it exists for python3.4+) or <a href="http://pythonhosted.org/joblib/" rel="nofollow"><code>joblib</code></a> (available with pip):</p>

<pre><code>from joblib import Parallel, delayed

def do_big_calculation(sub_list, b, c):
    return 1

if __name__ == '__main__':
    b, c = 1, 1
    ll = [([1, 2, 3, 4], b, c),
          ([5, 6, 7, 8], b, c),
          ([9, 10, 11, 12], b, c)]
    result = Parallel(n_jobs=-1)(
        delayed(do_big_calculation)(l, b, c) for l in ll)
    print(result)
</code></pre>

<p>The main advantage of such library is that it is developing whereas <code>multiprocessing</code> in python2.7 is freezed. Thus, there are bug fixes and improvements relatively often.<br>
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). </p>

python windows python-multiprocessing