i have program in need store global variable file. doing using pickle
module.
i have thread
(daemon=false
, threading
module) changes value of global variable. value modified in global scope(the main program).
i dumping value of variable .pkl
file every 5 seconds (using thread
threading
module).
but found following error when dump
method executed:
typeerror: can't pickle _thread.lock objects
why happening? , can fix it?
note: have found similar answers multiprocessing
module. need answer threading
module.
code:
def save_state(): while true: global variable lastsession = open('lastsession.pkl', 'wb') # error occurs on line pickle.dump(variable, lastsession) lastsession.close() time.sleep(5) state_thread = threading.thread(target = save_state) state_thread.setdaemon(false) state_thread.start() # variable changed outside function , in thread(not state_thread).
as others have mentioned, cannot pickle "volatile" entities (threads, connections, synchonization primitives etc.) because don't make sense persistent data.
looks you're trying saving session variables later continuation of said session. task, there's nothing can save objects that, well, cannot saved due nature.
the simplest solution ignore them. replacing them "stubs" yield error anytime them makes little sense, missing variable yields error anyway (it won't if masking global variable questionable practice in itself).
alternatively, can set these objects anew when restoring session. such logic necessity task-specific.
finally, e.g. ipython already has session saving/restoration logic, don't need reinvent wheel @ all.
Comments
Post a Comment