Python threading 线程跳不出来 后面代码无法继续执行 问题

发布于 2022-09-11 15:43:18 字数 1108 浏览 22 评论 0

遇到同样的问题,
线程调用的方法里 while True 就跳不出这个线程 结束while循环 while 外面的

def startsendfile(self,filelist,taskpath,filetypepath):

    for k in range(2):
        # new_thread = threading.Thread(target=self.booth(k,filelist,taskpath,filetypepath))
        new_thread = threading.Thread(target=self.booth,args=(k,filelist,taskpath,filetypepath))
        # new_thread.setDaemon(True)
        self.thread_list.append(new_thread)
    for t in self.thread_list:
        t.start()
    for t in self.thread_list:
        t.join()
    print("所有线程都结束了", time_now())

线程调用方法


def booth(self,tid, filelist,taskpath,filetypepath):
    global thread_data_num
    global lock
    while True:
        self.lock.acquire()
        print(self.thread_data_num)
        if self.thread_data_num != 0:
            self.thread_data_num = self.thread_data_num - 1
            item = filelist[self.thread_data_num]
            time.sleep(10)
        else:
            break
        self.lock.release()
    print("出来了么")
    

结果打印了===> 出来了么

却没有执行打印 ===> 所有线程都结束了

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2022-09-18 15:43:18

break前没有释放锁,死锁了

滴情不沾 2022-09-18 15:43:18

你给出的代码中thread_data_num一直为0,所以booth里面的循环会立即break,才会打印:出来了么,由于startsendfile中线程全部在t.join()位置阻塞,所以线程一直在等待中,不会打印:所有线程都结束了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文