Fixed file locking. Fix is slightly ugly (lock integer added) but it gets the job done

It avoids having to spin simply on the OSError.
This commit is contained in:
Gregory Becker 2015-08-25 15:32:45 -07:00
parent 4a2bd1753a
commit e32c59f805

View file

@ -115,22 +115,29 @@ def read_database(self):
""" """
if not self.is_dirty(): if not self.is_dirty():
return return
"""
while True: lock=0
while lock==0:
try: try:
os.mkdir(self.lock_path) os.mkdir(self.lock_path)
break lock=1
except OSError as err: except OSError as err:
pass pass
"""
#The try statement ensures that a failure won't leave the
#database locked to other processes.
try:
if os.path.isfile(self.file_path): if os.path.isfile(self.file_path):
with open(self.file_path,'r') as f: with open(self.file_path,'r') as f:
self.from_yaml(f) self.from_yaml(f)
else: else:
#The file doesn't exist, construct empty data. #The file doesn't exist, construct empty data.
self.data = [] self.data = []
except:
os.rmdir(self.lock_path)
raise
# os.rmdir(self.lock_path) os.rmdir(self.lock_path)
def write_database_to_yaml(self,stream): def write_database_to_yaml(self,stream):
@ -162,19 +169,25 @@ def write(self):
Write the database to the standard location Write the database to the standard location
Implements mkdir locking for the database file Implements mkdir locking for the database file
""" """
""" lock=0
while True: while lock==0:
try: try:
os.mkdir(self.lock_path) os.mkdir(self.lock_path)
break lock=1
except OSError as err: except OSError as err:
pass pass
"""
#The try statement ensures that a failure won't leave the
#database locked to other processes.
try:
with open(self.file_path,'w') as f: with open(self.file_path,'w') as f:
self.last_write_time = int(time.time()) self.last_write_time = int(time.time())
self.write_database_to_yaml(f) self.write_database_to_yaml(f)
except:
os.rmdir(self.lock_path)
raise
# os.rmdir(self.lock_path) os.rmdir(self.lock_path)
def get_index_of(self, spec): def get_index_of(self, spec):