I had some performance issues with a multi-threading code to parallelize multiple telnet probes.
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()
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()
I don't get why moving the start()
method change the performance so much.
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.