python - 如何暂停主线程直到完成一组并行线程的执行?
问题描述
首先,我是 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()
............
解决方案
由于您已经维护了列表threads
,您可以调用join
它的所有元素:
for thread in threads:
thread.join()
推荐阅读
- tensorflow - 框架暗网有多少模型?
- python - 机器翻译变压器输出 - “未知”令牌?
- go - 结构体和 GoRoutines,切换实例?
- .net - 是否可以使用 dotnet build/restore 在命令行上设置 TargetFramework?
- sqlite - SQLite 请求特定日期之间的日历事件
- xamarin - Xamarin Android DialogFragment 中的 OnTimedEvent() 如何 Dissmis()?
- hook - 在模式挂钩中向用户显示消息
- go - 构建约束排除所有 Go 文件 go build for amd64 系统
- python - 如何快速下载大量小文件?(不受带宽限制)
- javascript - 如何在打字稿中仅使用一种状态在 redux 存储中设置 preloadedState?