Python start all Threads from doulbe FOR LOOP
<p>I have this kind of scenario:</p>

<pre><code>threads = []
for wordToScan in wordsList:
    dictionaryFileOpen = open(dictionaryFile, "r")

    for i in range(10):
        threads.append(Thread(target=words_Scan_Start, args=(dictionaryFileOpen, wordToScan)))

for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

def words_Scan_Start(dictionaryFile, wordToScan):
    while True:
        sub_word = dictionaryFile.readline()
        if not sub_word:
            break
        #... Here is some actions.
</code></pre>

<p>Now I need to start all these threads at once(10 threads each for every <code>wordToScan in wordsList</code>).</p>

<p>Every <code>wordToScan in wordsList:</code> should use <code>dictionaryFileOpen</code> in 10 threads but without moving <code>.readline()</code> for the other <code>wordToScan in wordsList:</code> that working at the same time. Also, I can't understand where is to close the <code>dictionaryFileOpen</code>.</p>

<p>You should not read file from several threads. This involves following considerations:</p>

<ol>
<li>Objects which are accessed from several threads must be thread safe. File is not thread safe. So you must guard all places where you use it with mutex.</li>
<li>Reading from the disk is quite a slow process. A single thread can handle it with great margin.</li>
<li>Linear read is much faster than random read. So it is effective to read it from a single thread.</li>
</ol>

<p>So general approach is to read the file from one thread and then dispatch the processing fork for other threads. And if you can do the work in one thread it is better just to do it so, because it is much simpler. Make multithreaded applications only when you really need them.</p>

python multithreading readline