adplus-dvertising

Do something after all threads finished python ( using t.start()! ) [closed]

Asked 2 months ago
Viewed 24 times

Here is the code:

for tryproxy in proxy:
    threading.Thread(target=check, args=(tryproxy,)).start()
print("finished")

The problem is, even all threads finsished, it does not print out "finished".

asked 2 months ago

Correct Answer

To print out "finished" at the end you have to collect all thread objects in a list and wait for all of them to finish (using join method) before printing. This will look like:

threads = []

for tryproxy in proxy:
    t = threading.Thread(target=check, args=(tryproxy,))
    threads.append(t)
    t.start()

for t in threads:
    t.join()

print("finished")
answered 2 months ago