Skip to content

Commit

Permalink
filedict lock file now !
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed Jun 2, 2024
1 parent db86bac commit 9c3ca5a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions htagweb/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# https://github.com/manatlan/htagweb
# #############################################################################

import os,pickle,tempfile
import os,pickle,tempfile,fcntl

from collections import UserDict
class FileDict(dict): # default
Expand Down Expand Up @@ -42,14 +42,22 @@ def clear(self):

def _save(self):
if len(self):
with open(self._file,"wb+") as fid:
pickle.dump(dict(self),fid, protocol=4)
while 1:
with open(self._file,"wb+") as fid:
try:
fcntl.flock(fid, fcntl.LOCK_EX | fcntl.LOCK_NB)
pickle.dump(dict(self),fid, protocol=4)
break
except BlockingIOError:
pass
finally:
fcntl.flock(fid, fcntl.LOCK_UN)
os.chmod(self._file, 0o700)
else:
if os.path.isfile(self._file):
os.unlink(self._file)


class FilePersistentDict(FileDict): # default
def __init__(self,uid):
FileDict.__init__(self,uid,persistent=True)
Expand Down

0 comments on commit 9c3ca5a

Please sign in to comment.