S
S
Semyon Beloglazov2017-07-28 19:04:02
Python
Semyon Beloglazov, 2017-07-28 19:04:02

How to wait for threads to finish in Python?

Immediately I ask the admins not to close the topic because "it's easy to google." I googled - didn't find it.
Let's move on to the topic.
There is the following code:

def run(self):
        # Работа с прокси
        self.proxyList = self.jobProxy.split('\n') # Список прокси
        self.jobCount =  len(self.proxyList) # Количество прокси
        # Цикл по созданию потоков
        threads = []
        for i in range(self.jobCount):
            # Создаем новый поток и передаем ему 1 прокси
            t = threading.Thread(target = self.mainProccess, args = (self.proxyList.pop(0),))
            threads.append(t)
            t.start()
        # Конец цикла по созданию потоков
        print('Работа завершена!')

I'm new to python and haven't quite figured it out yet, but. I want the last print line to be printed only when all the threads in the for loop have finished.
The scheme should be as follows: we pass the proxy to the for loop > work with threads > if all the threads are over, then output print ()
And the following scheme comes out: the for loop begins > print is displayed > then the threads work on their own
How to implement synchronous code execution here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-07-28
@Batlab

...
for t in threads:
    t.join()
print('Работа завершена!')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question