长时间运行的python脚本的故障保护

时间:2013-02-06 00:10:56

标签: python

这可能是有史以来最愚蠢的问题,但我想知道是否有办法在函数(最好是装饰器)周围编写一个包装器,这样你就可以捕获局部变量的内部状态。该功能提出了一个例外。它会在创建本地时捕获它们,将它们腌制,然后在没有引发异常的情况下处理它们,或者如果发现任何异常则将它们写入文件。

这太过于幻想了,还是有人因这样的事情而被骗了?

2 个答案:

答案 0 :(得分:5)

您可以捕获f_locals variable on a frame in the traceback

import sys
import functools

def capturelocals(func):
    @functools.wraps(func)
    def wrapperfunc(*args, **kw):
        try:
            return func(*args, **kw)
        except Exception:
            _, _, tb = sys.exc_info()
            try:
                while tb.tb_next is not None:
                    tb = tb.tb_next  # find innermost frame
                locals = tb.tb_frame.f_locals
                print locals
            finally:
                del tb  # prevent leaking tracebacks
            raise
    return wrapperfunc

证明它有效:

>>> @capturelocals
... def foobar():
...     foo = 'bar'
...     spam = 'eggs'
...     raise ValueError('Bam!')
... 
>>> foobar()
{'foo': 'bar', 'spam': 'eggs'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in wrapperfunc
  File "<stdin>", line 5, in foobar
ValueError: Bam!

答案 1 :(得分:1)

另一种选择是将函数作为参数传递给你有兴趣保存的变量,以防万一发生灾难性事件。

可能帮助您完成此操作的装饰器:

import os
try:
    import cPickle as pickle
except ImportError:
    import pickle

def save_args_if_exception(pickle_folder):
    def real_decorator(function):
        def wrapper(*args, **kwargs):
            try:
                function(*args, **kwargs)
            except:
                print 'FAILING SAFE: Saving arguments to folder:'
                print pickle_folder
                for arg_position, arg in enumerate(args, start=1):
                    arg_fname = os.path.join(pickle_folder, str(arg_position) +
                                             '.pickle')
                    print 'Saving positional argument-{}'.format(arg_position)
                    with open(arg_fname, mode='wb') as f:
                        pickle.dump(arg, f)
                print 'Saving keyword arguments (kwargs)'
                with open(os.path.join(pickle_folder, 'kwargs.pickle'),
                          mode='wb') as f:
                    pickle.dump(kwargs, f)
                raise  # re-raise original exception
        return wrapper
    return real_decorator

@save_args_if_exception(os.getcwd())
def important_function(first_arg, second_arg=2, **kwargs):
    print 'About to raise an exception!'
    raise Exception

important_function(3)

请注意,在此示例中,second_arg未保存到文件中,可能是因为Python处理默认参数的方式。根据您的情况,这可能是也可能不是有用的行为。

参考文献: