首页 > 解决方案 > 如何暂停主线程直到完成一组并行线程的执行?

问题描述

首先,我是 python 多线程的新手。我有一个程序,它使用多个并行线程检测一组照片的手,并在检测到主程序后根据该手检测到的图像执行一些功能。函数在主线程上。但现在函数和手部检测线程都在并行运行。我需要暂停主线程,直到完成手动检测线程的执行。抱歉我的语言错误。

#Thread creation function

def callThreads(self, imageLst):
        global taskLock, tasks
        for tid in range(1, 10, 1):  ## 1,11,1
            thread = handdetect.detectHandThread(imageLst, tid, 'thread_{}'.format(tid), tasks, taskLock,
                                                 obj_detection_graph_queue, obj_detLock)
            thread.start()
            threads.append(thread)
            print("[INFO] Thread {} created and added to the pool...".format(tid))
# main() function of the programme
..............
self.callThreads(filenames)

Some functions()
............



标签: pythonmultithreading

解决方案


由于您已经维护了列表threads,您可以调用join它的所有元素:

for thread in threads:
  thread.join()

推荐阅读