不能用Ctrl-C杀死Python脚本。

时间:2022-10-20 22:43:01

I am testing Python threading with the following script:

我正在用下面的脚本测试Python线程:

import threading

class FirstThread (threading.Thread):
        def run (self):
                while True:
                        print 'first'

class SecondThread (threading.Thread):
        def run (self):
                while True:
                        print 'second'

FirstThread().start()
SecondThread().start()

This is running in Python 2.7 on Kubuntu 11.10. Ctrl+C will not kill it. I also tried adding a handler for system signals, but that did not help:

这是在Kubuntu 11.10上运行的Python 2.7。Ctrl+C不会杀死它。我还尝试为系统信号添加一个处理程序,但这并没有帮助:

import signal 
import sys
def signal_handler(signal, frame):
        sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

To kill the process I am killing it by PID after sending the program to the background with Ctrl+Z, which isn't being ignored. Why is Ctrl+C being ignored so persistently? How can I resolve this?

为了杀死这个过程,我将程序用Ctrl+Z发送到后台,然后用PID杀死它,这一点没有被忽略。为什么Ctrl+C总是被忽略?我该如何解决这个问题?

2 个解决方案

#1


149  

Ctrl+C terminates the main thread, but because your threads aren't in daemon mode, they keep running, and that keeps the process alive. We can make them daemons:

Ctrl+C会终止主线程,但是因为你的线程不是在守护进程中,所以它们会继续运行,并且保持进程的正常运行。我们可以让它们成为守护进程:

f = FirstThread()
f.daemon = True
f.start()
s = SecondThread()
s.daemon = True
s.start()

But then there's another problem - once the main thread has started your threads, there's nothing else for it to do. So it exits, and the threads are destroyed instantly. So let's keep the main thread alive:

但是,还有一个问题——一旦主线程启动了线程,就没有别的事情可做了。所以它退出,线程立即被销毁。所以我们保持主线程:

import time
while True:
    time.sleep(1)

Now it will keep print 'first' and 'second' until you hit Ctrl+C.

现在它会一直打印“第一个”和“第二个”,直到你按下Ctrl+C。

Edit: as commenters have pointed out, the daemon threads may not get a chance to clean up things like temporary files. If you need that, then catch the KeyboardInterrupt on the main thread and have it co-ordinate cleanup and shutdown. But in many cases, letting daemon threads die suddenly is probably good enough.

编辑:正如评论者指出的那样,守护线程可能没有机会清理像临时文件这样的东西。如果你需要它,那么就在主线程上捕捉键盘上的中断,让它协调清理和关闭。但在许多情况下,让守护进程线程突然死亡可能已经足够好了。

#2


7  

KeyboardInterrupt and signals are only seen by the process (ie the main thread)... Have a look at Ctrl-c i.e. KeyboardInterrupt to kill threads in python

键盘中断和信号只能通过进程看到(即主线程)……看一下Ctrl-c,即KeyboardInterrupt在python中杀死线程?

#1


149  

Ctrl+C terminates the main thread, but because your threads aren't in daemon mode, they keep running, and that keeps the process alive. We can make them daemons:

Ctrl+C会终止主线程,但是因为你的线程不是在守护进程中,所以它们会继续运行,并且保持进程的正常运行。我们可以让它们成为守护进程:

f = FirstThread()
f.daemon = True
f.start()
s = SecondThread()
s.daemon = True
s.start()

But then there's another problem - once the main thread has started your threads, there's nothing else for it to do. So it exits, and the threads are destroyed instantly. So let's keep the main thread alive:

但是,还有一个问题——一旦主线程启动了线程,就没有别的事情可做了。所以它退出,线程立即被销毁。所以我们保持主线程:

import time
while True:
    time.sleep(1)

Now it will keep print 'first' and 'second' until you hit Ctrl+C.

现在它会一直打印“第一个”和“第二个”,直到你按下Ctrl+C。

Edit: as commenters have pointed out, the daemon threads may not get a chance to clean up things like temporary files. If you need that, then catch the KeyboardInterrupt on the main thread and have it co-ordinate cleanup and shutdown. But in many cases, letting daemon threads die suddenly is probably good enough.

编辑:正如评论者指出的那样,守护线程可能没有机会清理像临时文件这样的东西。如果你需要它,那么就在主线程上捕捉键盘上的中断,让它协调清理和关闭。但在许多情况下,让守护进程线程突然死亡可能已经足够好了。

#2


7  

KeyboardInterrupt and signals are only seen by the process (ie the main thread)... Have a look at Ctrl-c i.e. KeyboardInterrupt to kill threads in python

键盘中断和信号只能通过进程看到(即主线程)……看一下Ctrl-c,即KeyboardInterrupt在python中杀死线程?