1

I have been searching a lot, and I cannot find whats wrong with my code. Its a proxy checker, and I have made a for loop with threading, and want it to print "FINISHED" after finishing the loop:

def cthread():
    while len(proxies) > 0:
        proxy = proxies.pop(0)
        try:
            requests.get(url, proxies={'https':proxytype+"://"+proxy}, timeout=timeout)
            print(proxy, '< GOOD PROXY')
            with open('working.txt', 'a') as proxywork:
                proxywork.write(proxy + '\n')
                proxywork.flush()
        except:
            try:
                print(proxy, ' > BAD')
            except:
                print("ERROR")
for i in range(tc):
    threading.Thread(target=cthread).start()
configuration.close()
print("FINISHED")
time.sleep(900.0 - ((time.time() - starttime) % 900.0))

but it prints "FINISHED" before even checking a half of the proxies, and I want it to do so after finishing the for loop.

Thanks for helping :)

ApolloGod
  • 13
  • 2
  • 1
    The loop *did* finish; what did not finish is all the threads *started* in the loop. You need to join each thread immediately after the loop to block until those threads complete. – chepner Apr 25 '20 at 15:34
  • oh I think I know what you mean, it finished because it could start all of them but I need to add threading.Thread(target=cthread).join() to the for loop to wait for them to finish? please correct me if wrong – ApolloGod Apr 25 '20 at 15:35

1 Answers1

2

You need to join each thread to wait for it to complete after the loop starts each thread.

threads = [threading.Thread(target=cthread) for _ in range(tc)]
for t in threads:
    t.start()

# Do stuff here while threads are running

# Now wait for all threads to complete
for t in threads:
    t.join()

configuration.close()
print("FINISHED")
time.sleep(900.0 - ((time.time() - starttime) % 900.0))
chepner
  • 497,756
  • 71
  • 530
  • 681