Python multi-threading performance issue related to start()
<p>I had some performance issues with a multi-threading code to parallelize multiple telnet probes.</p>

<h2>Slow</h2>

<p>My first implementation was is really slow, same a if the tasks were run sequencially:</p>

<pre><code>for printer in printers:
    â¦
    thread = threading.Thread(target=collect, args=(task, printers_response), kwargs=kw)
    threads.append(thread)

for thread in threads:
    thread.start()
    thread.join()
</code></pre>

<h2>Blastlingly Fast</h2>

<pre><code>for printer in printers:
    â¦
    thread = threading.Thread(target=collect, args=(task, printers_response), kwargs=kw)
    threads.append(thread)
    thread.start()  # &lt;----- moved this

for thread in threads:
    thread.join()
</code></pre>

<h2>Question</h2>

<p>I don't get why moving the <code>start()</code> method change the performance so much.</p>

<p>In your first implementation you are actually running the code sequentially because by <strong>calling <a href="https://docs.python.org/3/library/threading.html#threading.Thread.join" rel="nofollow"><code>join()</code></a> immediately after <a href="https://docs.python.org/3/library/threading.html#threading.Thread.start" rel="nofollow"><code>start()</code></a> the main thread is blocked</strong> until the started thread is finished.</p>

python performance python-3.x telnet python-multithreading