How to apply multiprocessing technique in python for-loop?
<p>I have a long list of user(about 200,000) and a corresponding data frame <code>df</code> with their attributes. Now I'd like to write a for loop to measure pair-wise similarity of the users. The code is following:</p>

<pre><code>df2record = pd.DataFrame(columns=['u1', 'u2', 'sim'])
for u1 in reversed(user_list):
    for u2 in reversed(list(range(1, u1))):
        sim = measure_sim(df[u1], df[u2]))
        if sim &lt; 0.6:
            continue
        else:
            df2record = df2record.append(pd.Series([u1, u2, sim], index=['u1', 'u2', 'sim']), ignore_index=True)
</code></pre>

<p>Now I wanna run this for loop with multiprocessing and I have read some tutorial. But I still have no idea to handle it properly. Seems that I should set reasonable number of processes first, like <code>6</code>. And then I should feed each loop into one process. But the problem is how can I know the task in a certain process has been done so that a new loop can begin? Could you help me with this? Thanks you in advance!</p>

<p>1st of all i would not recommend to use multiprocessing on such a small data. and especially when you are working with data frame. because data frame has it's own lot functionality which can help you in many ways. you just need to write proper loop.</p>

<p>Use: multiprocessing.Pool</p>

<p>just pass list of user as iterator(process_size=list_of_user) to pool.map() . you just need to create your iterator in a little tweak.</p>

<pre><code>from multiprocessing import Pool
with Pool() as pool:
     pool = multiprocessing.Pool(processes=6)
     pool.map(function, iterator)
</code></pre>

python multiprocessing