本文介绍了Re:如何杀死一个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! John Dohn写道: 当我这样做时,我在队列中放入一个特殊值(如None),并在 工作线程中,检查如果找到特殊值和退出。 线程也可以标记为守护程序线程 (参见 threading.Thread对象的文档)。如果 只有守护程序线程,这将使应用程序终止。剩下的。 所以最好的可能就像 - 在创建工作线程时调用setDaemon() - ... - 发送所有worker / daemon线程无队列 - 等待一段时间,比如time.sleep(1.0),让守护进程自己退出 - 退出主应用程序 - Gerhard 解决方案 天生地,你能用大多数语言做的最好的就是礼貌地问他们b / b 。否则你会在一个不方便的状态下留下锁和各种其他数据结构 ,这太复杂而无法正确处理。 例外是某些函数式语言,它们不是'能够在相同的意义上拥有线程和复杂状态的。 然而,你可以采用标准化的机制来实现目标。 礼貌地要求线程死亡。例如,可以使用所有阻塞I / O 函数来支持它。这是取消工作的方式 在python-safethread中。 John Dohn wrote:When I do this, I put a special value in the queue (like None) and inthe worker thread, check for the special value and exit if found.Threads can also be marked as "daemon threads" (see docs forthreading.Thread objects). This will make the application terminate ifonly "daemon threads" are left.So best would probably be soemthing like- call setDaemon() when creating worker threads- ...- send all worker/daemon threads None via queue- wait some time, like time.sleep(1.0), to let daemon exit themselves- exit main application-- Gerhard 解决方案Inherently, the best you can do in most languages is ask them politelyto die. Otherwise you''ll leave locks and various other datastructuresin an inconvenient state, which is too complex to handle correctly.The exception is certain functional languages, which aren''t capable ofhaving threads and complex state in the same sense.However, you could go quite far with a standardized mechanism ofpolitely asking threads to die. For instance, all blocking I/Ofunctions could be made to support it. This is how cancellation worksin python-safethread. 这篇关于Re:如何杀死一个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 08:05