python - 当另一个线程在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”字样
非常感谢您的回复
解决方案
推荐阅读
- javascript - 转换后的功能组件(具有角色权限的切换按钮)停止工作
- java - 如何在 Firebase 存储中上传位图屏幕截图并获取其下载 URL?(了解已弃用的 getDownloadURL())
- eclipse - Tomcat 应用程序使用完整的路径和资源,否则不能
- delphi - 如何从注册表 HKEY_LOCAL_MACHINE 中读取?
- oracle - 为什么 oracle 选择 INDEX FULL SCAN 然后选择 ACCESS BY INDEX ROWID vs FULL TABLE SCAN?
- java - JSON解析字符串失败,但Java中的数字通过
- node.js - 如何在节点中的 2 个嵌套循环后执行代码
- swift - 在 Swift 中计算方程
- python - 通过python将随机数据插入到具有MySQL数据库外键的表中
- python - Socks5 代理引发“连接意外关闭”和“[Errno 104] 对等连接重置”