Search code examples
pythonperformancepython-3.xtelnetpython-multithreading

Python multi-threading performance issue related to start()


I had some performance issues with a multi-threading code to parallelize multiple telnet probes.

Slow

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

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()

Blastlingly Fast

for printer in printers:
    …
    thread = threading.Thread(target=collect, args=(task, printers_response), kwargs=kw)
    threads.append(thread)
    thread.start()  # <----- moved this

for thread in threads:
    thread.join()

Question

I don't get why moving the start() method change the performance so much.


Solution

  • In your first implementation you are actually running the code sequentially because by calling join() immediately after start() the main thread is blocked until the started thread is finished.