【问题标题】:checkpointing in python to catch the runtime statepython中的检查点以捕获运行时状态
【发布时间】:2017-05-24 05:42:13
【问题描述】:

我有一个问题要使我的代码更易于自我修复。例如:我执行方法 1 将数据从 CSV 加载到 Vertica 数据库中。我有另一种方法 2 来检查数据库中的行数和 CSV 文件中的行数是否相同。如果行数不匹配,那么我正在考虑从调用查询的位置调用方法 2,以将数据从 CSV 加载到数据库中。

我正在考虑针对这个问题的检查点策略。比如,在代码中维护一些通常会发生错误的点,并在其他点调用它们。

我已经尝试在 python 中使用 pickle 模块,但后来知道 pickle 只能保存对象、类、变量等。无法保存我可以实际执行方法的点。

我提供了一些演示代码:

import pickle        
class Fruits:
  def apple(self):
    filehandler= open ("Fruits.obj","wb")
    print "apple"
    pickle.dump(self,filehandler)
    print "mapple"
    filehandler.close()
  def mango(self):
    filehandler = open("Fruits.obj","rb")
    print "mango"
    obj=pickle.load(filehandler)
    obj.apple()

general = Fruits()
general.apple()
general.mango()

the output of above program is:
apple
mapple
mango
apple
mapple

我希望我的代码执行时,当 mango 方法调用 apple 方法时,它必须从仅打印“mapple”的点开始执行。它不能执行整个方法。

请给我一些关于如何解决这个问题的见解。

提前致谢

【问题讨论】:

    标签: python pickle vertica checkpointing self-healing


    【解决方案1】:

    注意
    您的代码根本不起作用。 filehandler in def mango(... IS NOTdef apple(... 中的 filehandler 相同。因此,def mango(... 中打开的文件永远不会关闭

    if condidtion 添加到def apple,您根本不需要pickle

    def apple(self, mango=False):
        if not a´mango:
            filehandler= open ("Fruits.obj","wb")
            ...
    
        print "mapple"        
        ...
    
     def mango(self):
        filehandler = open("Fruits.obj","rb")
        ...
        obj.apple(True)
    

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多