首页 > 解决方案 > 当另一个线程在python中完成时如何停止一个线程?

问题描述

我有一个带有 2 个线程的 python 代码。我希望它们同时开始和执行。但是,当第一个完成时,即使没有完成,也希望第二个停止。

这是我的代码:

def process_one():
      i = 0
      while i < 10:
      print('00')
      i += 1

def process_two(stop):
     while stop == True:
     print('XX')


#Création des thread
th1 = threading.Thread(target=process_one)
stop = th1.isAlive()
while th1.isAlive()==True:
    stop = True
th2 = threading.Thread(target=process_two(stop))
th2.start()
th1.start()

th1.join()
th2.join()

print('Fin du programme')

这就是结果:00 00 00 00 00 00 00 00 00 00 Fin du program

我不明白为什么没有“XX”字样

非常感谢您的回复

标签: python

解决方案


推荐阅读