Parallelism inside of a function?
<p>I have a function that counts how often a list of items appears in <code>rows</code> below:</p>

<pre><code>def count(pair_list):
    return float(sum([1 for row in rows if all(item in row.split() for item in pair_list)]))

if __name__ == "__main__":
    pairs = [['apple', 'banana'], ['cookie', 'popsicle'], ['candy', 'cookie'], ...]
    # grocery transaction data
    rows = ['apple cookie banana popsicle wafer', 'almond milk eggs butter bread', 'bread almonds apple', 'cookie candy popsicle pop', ...]

    res = [count(pair) for pair in pairs]
</code></pre>

<p>In reality, <code>len(rows)</code> is <code>10000</code> and there are <code>18000</code> elements in <code>pairs</code>, so the computing cost of the list comprehension in <code>count()</code> and the one in the main function is expensive.</p>

<p>I tried some parallel processing:</p>

<pre><code>from multiprocessing.dummy import Pool as ThreadPool
import multiprocessing as mp

threadpool = ThreadPool(processes = mp.cpu_count())

res = threadpool.map(count, pairs)
</code></pre>

<p>This doesn't run quickly, either. In fact, after 15 minutes, I just quit the job because it didn't look to be ending. Two questions: 1) how can I speed up the actualy searching that takes place in <code>count()</code>? 2) how can I check the status of the <code>threadpool.map</code> process (i.e. see how many pairs are left to iterate over)?</p>

<p>1) The overall complexity of calculations is enormous, and it comes from different sources:</p>

<p>a) You split row on low level of calculation, so python has to create new row split for every iteration. To avoid this, you can pre-calculate rows. Something like this will do the job (with minor changes in "count" function):</p>

<pre><code>rows2 = [row.split() for row in rows]
</code></pre>

<p>b) You compare list items one by one, even though you only need to check existence of word in another list. Here we can tweak it more (and use rows3 instead of rows2 in "count" function):</p>

<pre><code>rows3 = [set(row.split()) for row in rows]

def count(pair_list):
    return float(sum([1 for row in rows3 if all(item in row for item in pair_list)]))
</code></pre>

<p>c) You check every word in pairs with every word in rows. Calculation takes 2*len(row)*len(rows) iterations per call of "count" function for original version, while it can take less. For option b) it can be down to 2*len(rows) in good case, but it's possible to make one set lookup per pair, not 2.
The trick is to make preparation of all possible word*word combinations for every row and check if corresponding tuple exists in this set. 
So, in main function you create complex immutable search structure:</p>

<pre><code>rows4 = [set((a, b) for a in row for b in row) for row in rows2]
</code></pre>

<p>And now "count" will look different, it takes tuple instead of list:</p>

<pre><code>def count2(pair):
    return float(len([1 for row in rows4 if(pair in row)]))
</code></pre>

<p>So you call it a bit different:
    res = [count2(tuple(pair)) for pair in pairs]</p>

<p>Note that search structure creation takes len(row.split())^2 per row in time and space, so if your row can be long, it's not optimal. After all, option b) can be better.</p>

<p>2) You can predict number of calls for "count" - it's len(pairs). Count calls of "count" function and make debug print in it for, say, every 1000 calls. </p>

python multithreading list parallel-processing