如何在 python 中保存多个会话状态
How to save multiple session states in python
我的 python 代码需要在退出时保存一个对象的状态,以便 运行 稍后可以在使用该对象时恢复。我正在使用泡菜这样做。下面是我的代码的 pickle 部分:
#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 传递文件名。
这个问题还有其他干净的解决方案吗?
将 pickle 文件保存为升序编号的文件名。
然后当你启动一个模块时,锁定具有最小数字的文件,因为你知道这是你必须继续的第一个过程。过程进行时不要释放锁。
所以其他进程没有访问这个文件,将搜索下一个。
您可以为您的 NewState
class 定义一个 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
class 的另一个属性。
- 理想情况下,您将使用
multiprocessing
处理各种流程。有关示例,请参阅 here。
我的 python 代码需要在退出时保存一个对象的状态,以便 运行 稍后可以在使用该对象时恢复。我正在使用泡菜这样做。下面是我的代码的 pickle 部分:
#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 传递文件名。
这个问题还有其他干净的解决方案吗?
将 pickle 文件保存为升序编号的文件名。 然后当你启动一个模块时,锁定具有最小数字的文件,因为你知道这是你必须继续的第一个过程。过程进行时不要释放锁。 所以其他进程没有访问这个文件,将搜索下一个。
您可以为您的 NewState
class 定义一个 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
class 的另一个属性。 - 理想情况下,您将使用
multiprocessing
处理各种流程。有关示例,请参阅 here。