假设我运行了一个 Python 程序,它在执行过程中到达了一个特定的点。我希望能够对该状态进行“快照”,以便能够在将来运行它。
例如:
- 我运行 test1.py 来创建对象、会话等,并点击断点 1。
- 我拍摄了 Python 进程的“快照”,然后继续执行该程序。
- 在稍后阶段,我希望能够从“快照”恢复并从断点 1 开始执行程序。
我为什么要这个?重复执行一个特定的任务,如果开始很平凡,只有结束很有趣,那么我不想每次都浪费时间运行第一部分。
有什么建议或指示我如何做到这一点,或者我应该查看哪些工具?
假设我运行了一个 Python 程序,它在执行过程中到达了一个特定的点。我希望能够对该状态进行“快照”,以便能够在将来运行它。
例如:
我为什么要这个?重复执行一个特定的任务,如果开始很平凡,只有结束很有趣,那么我不想每次都浪费时间运行第一部分。
有什么建议或指示我如何做到这一点,或者我应该查看哪些工具?
在我看来,您需要一些可以持久记忆的东西。这是基本的,但可能会让您入门:
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