1
600
在python和任何语言中,突然终止线程通常都是一种糟糕的模式。请考虑以下情况:
如果您负担得起(如果您管理自己的线程),处理这个问题的好方法是有一个exit_request标志,每个线程都会定期检查这个标志,看是否是时候让它退出了。 例如:
在这段代码中,您应该在希望线程退出时对其调用stop(),并使用join()等待线程正确退出。线程应定期检查停止标志。 但也有一些情况下,你真的需要杀死一个线程。一个例子是,当您包装一个外部库时,该库正忙着长时间的调用,并且您想中断它。 以下代码允许(有一些限制)在python线程中引发异常:
基于
Killable Threads
托默·菲利巴。关于
如文档中所述,这不是一个神奇的项目符号,因为如果线程在python解释器之外很忙,它将不会捕捉到中断。 这段代码的一个好的使用模式是让线程捕获一个特定的异常并执行清理。这样,你可以中断一个任务,仍然有适当的清理。 |
2
105
没有官方的api可以做到这一点,不。 您需要使用平台api来终止线程,例如pthread_kill或terminatethread。您可以通过pythonwin或cTypes访问此类API。 请注意,这本质上是不安全的。它可能会导致无法收集的垃圾(来自成为垃圾的堆栈帧的局部变量),如果被杀死的线程在被杀死的时候具有gil,则可能导致死锁。 |
3
78
一
如果我想杀死一个线程,但不想使用flags/locks/signals/semaphores/events/whatever,我会将线程提升为完全成熟的进程。对于只使用几个线程的代码,开销并没有那么大。 例如,这可以方便地终止执行阻塞I/O的助手“线程”
转换很简单:在相关代码中全部替换
|
4
61
如果试图终止整个程序,可以将线程设置为“守护进程”。看见 Thread.daemon |
5
33
这是基于 thread2 -- killable threads (Python recipe) 您需要调用Pythreadstate_setAsyncExc(),它仅通过cTypes可用。 这只在Python2.7.3上进行过测试,但它很可能适用于其他最近的2.x版本。
|
6
32
你不应该不配合就强行杀死一根线。 终止线程将删除try/finally块设置的任何保证,这样您可能会将锁锁定、文件打开等。 你唯一能说强制杀死线程是个好主意的时候,就是快速杀死一个程序,但不要只杀死一个线程。 |
7
29
正如其他人所提到的,标准是设置一个停止标志。对于轻量级的东西(没有线程的子类化,没有全局变量),lambda回调是一个选项。(注意括号
替代
(仅在windows/eclipse/python3.3上测试) |
8
21
在python中,不能直接杀死线程。 如果你真的不需要线程(!),而不是使用 threading package ,即使用 multiprocessing package . 在这里,要终止进程,只需调用方法:
python将终止您的进程(在unix上通过sigterm信号,而在windows上通过
请注意
如果你真的需要使用线程,没有办法直接杀死它。但是,您可以做的是使用 “守护进程线程” . 实际上,在python中,线程可以标记为 守护进程 :
当没有活动的非守护进程线程时,主程序将退出。换句话说,当主线程(当然是非守护进程线程)完成其操作时,即使仍有一些守护进程线程在工作,程序也将退出。
注意,有必要将线程设置为
你当然可以也应该
最后,请注意
|
9
13
可以通过在将退出线程的线程中安装跟踪来终止线程。一个可能的实现见附件链接。 |
10
9
如果你不杀线就更好了。 一种方法可以是在线程周期中引入一个“try”块,并在希望停止线程时抛出一个异常(例如break/return/……这就阻止了你的。 我已经在我的应用程序中使用过这个,它可以工作… |
11
8
完全有可能实现
这个
|
12
6
T
是你的
阅读python源代码(
|
13
5
以下解决方法可用于终止线程:
这甚至可以用于从主线程终止其代码在另一个模块中写入的线程。我们可以在该模块中声明一个全局变量,并使用它终止在该模块中生成的线程。 我通常使用这个来终止程序出口处的所有线程。这可能不是终止线程的完美方法,但可能会有所帮助。 |
14
4
我想补充一点,如果你在 threading lib Python ,建议避免使用“恶魔”线程,当您不希望线程突然结束时,使用paolo rovelli标志 mentioned 。 官方文件:
我认为创建守护线程取决于您的应用程序,但通常(在我看来)最好避免杀死它们或使它们成为守护线程。在多处理中,您可以使用
永远记住,如果有“活线程”,python解释器将运行等待它们。(因为这个守护可以帮助你如果不要紧的话突然结束)。 |
15
4
我来晚了,但我一直在努力 a similar question 下面的代码似乎可以很好地解决这个问题,并让我在守护子线程退出时执行一些基本的线程状态检查和清理:
产量:
|
16
3
如果您明确地调用
例如:
然后运行它
使用的好处
|
17
1
有一个图书馆是为这个目的而建的, stopit . 尽管这里列出的一些相同的警告仍然适用,但是至少这个库提供了一种常规的、可重复的技术来实现所述的目标。 |
18
0
这似乎适用于windows 7上的pywin32
|
19
0
以下是操作方法:
给它几秒钟,然后你的线程应该停止。同时检查
我建议你
|
20
0
使用setdaemon(true)启动子线程。
|
21
0
虽然它很老, this 对某些人来说可能是个方便的解决方案:
因此,它允许一个“线程在另一个线程的上下文中引发异常”,这样,终止的线程就可以在不定期检查中止标志的情况下处理终止。 然而,根据其 original source ,此代码有一些问题。
|
22
0
Pieter Hintjens是 ÃMQ -project——也就是说,使用mq并避免同步原语(如锁、互斥锁、事件等)是编写多线程程序的最明智和最安全的方法: http://zguide.zeromq.org/py:all#Multithreading-with-ZeroMQ 这包括告诉子线程它应该取消它的工作。这可以通过为线程配置mq套接字并在该套接字上轮询一条消息来完成,该消息表示该线程应该取消。 该链接还提供了一个使用mq的多线程python代码的示例。 |
23
-1
您可以在进程中执行命令,然后使用进程id终止它。 我需要在两个线程之间同步其中一个线程本身不返回。
|
24
-2
如果您真的需要终止子任务的能力,请使用另一个实现。
python的线程不支持取消。甚至不要尝试。您的代码很可能会死锁、损坏或泄漏内存,或具有其他意外的“有趣”难以调试的效果,这种效果很少发生,而且不确定。 |
Ma Joonyoung · 粗粒度和细粒度链表的时间比较 1 年前 |
user1700890 · 了解交互式代理Python API中的线程 2 年前 |
AntonBoarf · 为什么要将实例变量指定给局部变量? 2 年前 |
rhymes · 如何让线程操作相同的java列表 2 年前 |