SIGINT 后保存工作

发布于 2024-08-09 10:54:58 字数 695 浏览 10 评论 0原文

我有一个需要很长时间才能完成的程序。我想 它能够捕获 SIGINT (ctrl-c) 并调用 self.save_work() 方法。

就目前情况而言,我的 signal_hander() 无法正常工作 当程序到达 signal_handler() 时,self 尚未定义。

我如何设置它以便在 SIGINT 之后调用 self.save_work

#!/usr/bin/env python
import signal 

def signal_handler(signal, frame):    
    self.save_work()   # Does not work
    exit(1)
signal.signal(signal.SIGINT, signal_handler)

class Main(object):
    def do_stuff(self):
        ...
    def save_work(self):
        ...
    def __init__(self):
        self.do_stuff()
        self.save_work()

if __name__=='__main__':
    Main()

I have a program which takes a long time to complete. I would like
it to be able to catch SIGINT (ctrl-c) and call the self.save_work() method.

As it stands, my signal_hander() does not work since
self is not defined by the time the program reaches signal_handler().

How can I set it up so self.save_work gets called after a SIGINT?

#!/usr/bin/env python
import signal 

def signal_handler(signal, frame):    
    self.save_work()   # Does not work
    exit(1)
signal.signal(signal.SIGINT, signal_handler)

class Main(object):
    def do_stuff(self):
        ...
    def save_work(self):
        ...
    def __init__(self):
        self.do_stuff()
        self.save_work()

if __name__=='__main__':
    Main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔嚣张 2024-08-16 10:54:58

如果您只想捕获 ctr+c 那么您可以捕获 KeyboardInterrupt 例外:

class Main(object):
    def do_stuff(self):
        ...
    def save_work(self):
        ...
    def __init__(self):
        try:
            self.do_stuff()
        except KeyboardInterrupt:
            pass # Or print helpful info
        self.save_work()

我并不认为这是一个好的设计。看来您需要使用函数而不是构造函数。

If you just want to catch ctr+c then you can catch the KeyboardInterrupt exception:

class Main(object):
    def do_stuff(self):
        ...
    def save_work(self):
        ...
    def __init__(self):
        try:
            self.do_stuff()
        except KeyboardInterrupt:
            pass # Or print helpful info
        self.save_work()

Not that I think this is a good design after all. It looks like you need to be using a function instead of a constructor.

悲歌长辞 2024-08-16 10:54:58

通常,“工作”涉及某种大循环。要驯服循环并防止其在未知步骤中中断,您可以使用以下上下文管理器:

import signal

class GracefulInterruptHandler(object):

    def __init__(self, sig=signal.SIGINT):
        self.sig = sig

    def __enter__(self):

        self.interrupted = False
        self.released = False

        self.original_handler = signal.getsignal(self.sig)

        def handler(signum, frame):
            self.release()
            self.interrupted = True

        signal.signal(self.sig, handler)

        return self

    def __exit__(self, type, value, tb):
        self.release()

    def release(self):

        if self.released:
            return False

        signal.signal(self.sig, self.original_handler)

        self.released = True

        return True

使用:

import time

/// do stuff:
with GracefulInterruptHandler() as h:
    for i in xrange(1000):
        print "..."
        time.sleep(1)
        if h.interrupted:
            print "interrupted!"
            time.sleep(5)
            break

save_work()

从此处: https://gist.github.com/2907502

Usually, "work" involves some kind of a big loop. To tame your loop, and prevent it from breaking in an unknown step, you can use the following context manager:

import signal

class GracefulInterruptHandler(object):

    def __init__(self, sig=signal.SIGINT):
        self.sig = sig

    def __enter__(self):

        self.interrupted = False
        self.released = False

        self.original_handler = signal.getsignal(self.sig)

        def handler(signum, frame):
            self.release()
            self.interrupted = True

        signal.signal(self.sig, handler)

        return self

    def __exit__(self, type, value, tb):
        self.release()

    def release(self):

        if self.released:
            return False

        signal.signal(self.sig, self.original_handler)

        self.released = True

        return True

To use:

import time

/// do stuff:
with GracefulInterruptHandler() as h:
    for i in xrange(1000):
        print "..."
        time.sleep(1)
        if h.interrupted:
            print "interrupted!"
            time.sleep(5)
            break

save_work()

From here: https://gist.github.com/2907502

蓝天白云 2024-08-16 10:54:58
import signal

def signal_handler(signal, frame):    
   #do some stuff

def main():
   #do some more stuff


if __name__=='__main__':
    signal.signal(signal.SIGINT, signal_handler)
    main()
import signal

def signal_handler(signal, frame):    
   #do some stuff

def main():
   #do some more stuff


if __name__=='__main__':
    signal.signal(signal.SIGINT, signal_handler)
    main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文