如何在python中保存多个会话状态

时间:2015-07-29 13:57:45

标签: python pickle

我的python代码需要在退出时保存一个对象的状态,以便稍后可以使用此对象恢复运行。我这是用泡菜做的。以下是我的代码的腌菜部分:

#At the begining of the file
pickle_file = '.state_pickle.obj'
#if pickle file is present load object, else create new
if os.path.exists(pickle_file):
    with open(pickle_file, 'r') as fh:
        state = pickle.load(fh)
else:
    state = NewState() #NewState is a class
....
....
#At the end of the file
with open(pickle_file, 'w') as fh:
    pickle.dump(state, fh)

# When -stop option is given, then the session is stopped
# The pickle file is deleted at that time
if args.stop:
    if os.path.exists(pickle_file):
        os.remove(pickle_file)
    ...

这对我来说很好。但是,从同一目录打开多个会话时出现问题。 pickle_file ('.state_pickle.obj')文件被覆盖导致错误的结果。有没有办法将obj保存为唯一的文件名,以便在恢复会话时可以读取文件。此外,我甚至需要在解析args之前获取state对象。所以,我无法通过args传递文件名。 这个问题还有其他清洁解决方案吗?

2 个答案:

答案 0 :(得分:1)

您可以为NewState类定义 id 属性,然后使用id创建方法,看起来像这样:

import glob
def get_new_id(source_folder):
    new_id = 0
    for a_file in glob.glob(source_folder):
        # assuming a pickle file looks like 'some/path/13.obj' 
        # with 'some/path' == source_folder and 13 some id of a state.
        an_id = int(a_file.split('/')[-1].replace('.obj', ''))
        if an_id >= new_id:
            new_id = an_id + 1
    return new_id

然后,您必须知道要恢复的状态的ID,或者只知道最后一个状态:

# get the last state:
last_id = get_new_id(path_to_pickles)
pickle_file = os.path.join(path_to_pickles, '%s.obj' % str(last_id))
if os.path.exists(pickle_file):
    with open(pickle_file, 'r') as fh:
        state = pickle.load(fh)
else:
    state = NewState() #NewState is a class
    state.id = last_id

然后在结束时:

pickle_file = os.path.join(path_to_pickles, '%s.obj' % str(state.id))
with open(pickle_file, 'w') as fh:
    pickle.dump(state, fh)

# When -stop option is given, then the session is stopped
# The pickle file is deleted at that time
pickle_file = os.path.join(path_to_pickles, '%s.obj' % str(state.id))
if args.stop:
    if os.path.exists(pickle_file):
        os.remove(pickle_file)
    ...

PS。

  • 我可能将pickle_file定义为NewState类的另一个属性。
  • 理想情况下,您将使用multiprocessing处理各种流程。有关示例,请参阅here

答案 1 :(得分:0)

将pickle文件保存为升序编号的文件名。 然后,当您启动模块时,锁定具有最小编号的文件,因为您知道这是您必须继续的第一个过程。在此过程继续进行时,不要释放锁定。 所以其他进程无法访问此文件,将搜索下一个文件。