【问题标题】:Run same thread in loop在循环中运行相同的线程
【发布时间】:2020-06-01 22:12:04
【问题描述】:

我想多次运行下面的线程,每次它完成时都将结果打印到 tkinter 的标签。

    def hasher(path):

        hashed_file = self.hash256(path)
        tk.Label(self, text=hashed_file).pack()

    t = []
    for directory in files_dict:

        for file in files_dict[directory]:

            t.append(threading.Thread(target=hasher, args=(directory+"/"+file,)))

    for thr in t:
        thr.start()
        thr.join()

基本上是为了以后能够更新带有“已完成处理:”的标签 但是,如果我添加 .join() 它会冻结,如果我删除它会完成然后加载页面。

【问题讨论】:

    标签: python multithreading tkinter


    【解决方案1】:

    在最后一个 for 循环中,您从列表 tthr.start() 开始一个线程 thr,然后等待它,防止列表 t 中的其他线程首先启动。 thr.join() 语句需要一个额外的循环:

    for thr in t: thr.start() for thr in t: thr.join()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 2021-07-07
      • 2023-03-06
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多