Python:无法通过键盘中断杀死进程?

时间:2019-02-01 02:57:38

标签: python kill

我有这样写的装饰器:

import threading
from time import sleep
from functools import wraps
import sys
import os

def repeat_periodically(f):
    """ Repeat wrapped function every second """
    @wraps(f)
    def wrap(self, *args, **kwargs):
        def wrap_helper(*args, **kwargs):
            try:
                threading.Timer(1.0, wrap_helper).start()
                f(self)
            except KeyboardInterrupt:
                try:
                    sys.exit(1)
                except:
                    os._exit(1)

        wrap_helper()

    return wrap

我不确定每次调用它是否都会继续打开新线程,但是无论如何,当我点击CTRL + C时,我将无法终止该进程。我还在装饰的函数中添加了相同的try-except块:

@repeat_periodically
def get_stats(self):
    try:
        # log some state information
    except KeyboardInterrupt:
        try:
            sys.exit(1)
        except:
            os._exit(1)

我的程序继续运行,我在终端上看到的只是

^C <the stuff that I am logging>
<the stuff that I am logging>
<the stuff that I am logging>

换句话说,即使我试图用CTRL + C杀死它,它仍然保持记录。

更新

我应该提到上面的过程是从另一个线程中旋转出来的:

tasks = [
    {'target': f, 'args': (arg1)},
    {'target': g},
]
for task in tasks:
    t = threading.Thread(**task)
    t.start()

具体来说,这是第二个启动计时器的任务。但是,如果我设置t.daemon = True,则该过程仅运行一次并退出。第一个任务使用watchdog。我基本上使用了看门狗文档中的示例代码:

def watch_for_event_file(Event):
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingCreateHandler(Event)
    observer = Observer()
    observer.schedule(event_handler, path)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

(对不起所有更新)

1 个答案:

答案 0 :(得分:2)

从线程documentation

The entire Python program exits when no alive non-daemon threads are left.

因此,将Timer线程作为守护程序线程应该可以解决您的问题。因此,替换:

threading.Timer(1.0, wrap_helper).start()

具有:

t = threading.Timer(1.0, wrap_helper)
t.daemon = True
t.start()