2013-06-01 40 views
0

比方说,我运行一个Python程序,并在执行该程序时得到一个特定的点。我希望能够将这个状态的“快照”能够在未来的某个点上运行。快照Python进程并在稍后进行恢复

如:

  • 我跑test1.py肚里有关创建对象,会话等,命中断点-1。
  • 我拍摄了Python进程的“快照”,然后继续执行程序。
  • 在稍后阶段,我希望能够从“快照”中恢复并从断点1开始执行程序。

为什么我要这个?要重复执行一个特定的任务,如果开始非常平凡,只有结束才有意思,那么我不想浪费时间来运行第一部分。

任何建议,或指示我如何做到这一点,或我应该看什么工具?

+0

像调试? – fvrghl

+2

或者像pickle这样的持久数据存储来保存中间值? –

+0

我建议传递一个命令行参数来告诉它是否跳过最初的东西。除非你真的真的需要一个通用的解决方案,然后看看泡菜 – ahuff44

回答

0

这听起来像是你需要一些持久性记忆。这是初级的,但可能让你开始:

import shelve 

class MemoizedProcessor(object): 
    def __init__(self): 
    # writeback only if it can't be assured that you'll close this shelf. 
    self.preprocessed = shelve.open('preprocessed.cache', writeback = True) 
    if 'inputargs' not in self.preprocessed: 
     self.preprocessed['inputargs'] = dict() 

    def __del__(self, *args): 
    self.preprocessed.close() 

    def process(self, *args): 
    if args not in self.preprocessed['inputargs']: 
     self._process(*args) 
    return self.preprocessed['inputargs'][args] 

    def _process(self, *args): 
    # Something that actually does heavy work here. 
    result = args[0] ** args[0] 
    self.preprocessed['inputargs'][args] = result 
相关问题