简体   繁体   English

在python中检查点以捕获运行时状态

[英]checkpointing in python to catch the runtime state

I have a problem to make my code more self-healable. 我有一个问题使我的代码更容易修复。 Eg: I execute a method 1 to load the data from a CSV into the Vertica database. 例如:我执行方法1,将数据从CSV加载到Vertica数据库中。 I have another method 2 to check if the number of rows in the database and the number of lines in CSV file is same. 我有另一种方法2来检查数据库中的行数和CSV文件中的行数是否相同。 If the number of lines doesn't match, then I was thinking of calling the method 2 from the point where it called the query to load data from CSV into the database. 如果行数不匹配,那么我正在考虑从方法2调用查询以将数据从CSV加载到数据库中开始。

I was thinking of a checkpointing strategy for this problem. 我当时正在考虑针对此问题的检查点策略。 like, maintain some points in the code where the errors usually occur and recalling them at other points. 例如,在代码中维护通常会发生错误的某些点,并在其他地方重新调用它们。

I already tried using pickle module in python, but came to know that pickle can only save objects, classes, variables etc. can't save the point from where I can actually execute a method. 我已经尝试在python中使用pickle模块,但后来发现pickle只能保存对象,类,变量等。不能保存我实际可以执行方法的地方。

i have provided some demo code: 我提供了一些演示代码:

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

I want my code to execute such that when mango method calls apple method, it must execute from the point of only print "mapple". 我希望我的代码能够执行,以便当芒果方法调用apple方法时,它必须仅从打印“ mapple”的角度执行。 it must not execute the whole method. 它不能执行整个方法。

please do provide me some insight on how to solve this problem. 请提供一些有关如何解决此问题的见解。

thanks in advance 提前致谢

Note : 注意事项
Your code doesn't work at all. 您的代码根本不起作用。 filehandler in def mango(... IS NOT the same as filehandler in def apple(... . Therfore, the file opend in def mango(... are never closed . filehandlerdef mango(... 是不一样filehandlerdef apple(... 。Therfore,在该文件中运行结束def mango(...从来不关

Add a if condidtion to def apple , you don't need pickle at all. def apple添加if condidtion ,则完全不需要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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python捕获运行时错误类型 - Python catch runtime errortype 在python中捕获异常/运行时错误 - Catch exceptions/runtime errors in python 在Python中只捕获一些运行时错误 - Catch only some runtime errors in Python python,multiprocessing和dmtcp:在Pool中检查一个进程? - python, multiprocessing and dmtcp: checkpointing one process in Pool? Azure Eventhubs (Python):使用 blob 存储进行检查点 - 启用检查点时 EventProcessor 中的 keyerror 问题 - Azure Eventhubs (Python): checkpointing with blob storage - keyerror issue in EventProcessor when checkpointing is enabled 如何捕获用C编写的python模块的运行时错误? - How to catch runtime error for a python module written in C? 如何从python中的本机代码中捕获运行时错误? - How to catch runtime errors from native code in python? 如何在python中运行时捕获断言(在C ++中引起) - How to catch assertion(caused in C++) during runtime in python Spark Worker 因致命 Python 错误而崩溃:已到达无法访问的 C 代码路径。 Python 运行时 state:已初始化 - Spark Worker Crashed with Fatal Python error: Unreachable C code path reached. Python runtime state: initialized 使用Bottle捕获mako运行时错误 - Catch mako runtime errors using Bottle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM