Skip to content

Commit

Permalink
Implement non fnctl.flock() file locking
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolphpienaar committed Dec 28, 2023
1 parent fc76273 commit 2ac17b6
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions pypx/smdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
import time
import pudb

from pathlib import Path


def parser_setup(str_desc):
parser = ArgumentParser(
description = str_desc,
Expand Down Expand Up @@ -740,26 +743,32 @@ def seriesData(self, str_table, *args) -> dict:
"""

# @retry(Exception, delay = 1, backoff = 2, max_delay = 4, tries = 10)
def seriesData_write(str_filename, d_obj):
@retry(Exception, delay = 1, backoff = 2, max_delay = 4, tries = 10)
def seriesData_safeWrite(str_filename, d_obj) -> dict:
return seriesData_write(str_filename, d_obj)

def seriesData_write(str_filename, d_obj) -> dict:
"""
Multiprocess safe write
"""
d_ret : dict = {
'status' : True,
'error' : ""
}

try:
# Create a lock file. If this already exists, the touch()
# will raise an exception.
lockFile:Path = Path(str_filename).with_suffix('.lock')
lockFile.touch(exist_ok = False)
with open(str_filename, 'w') as fj:
# Lock it!
fcntl.flock(fj, fcntl.LOCK_EX | fcntl.LOCK_NB)
# Edit it!
self.json_write(d_obj, fj)
# Unlock it!
fcntl.flock(fj, fcntl.LOCK_UN)
except Exception as e:
# Now that we've written the data, remove the lock!
lockFile.unlink()
except FileExistsError as e:
d_ret['status'] = False
d_ret['error'] = "Possible multiprocess write failure."
raise
return d_ret

b_status : bool = False
Expand Down Expand Up @@ -815,7 +824,7 @@ def seriesData_write(str_filename, d_obj):
if b_canWrite:
d_meta[str_field] = value
str_fileName = d_seriesTable[str_tableName]['name']
d_write = seriesData_write(str_fileName, d_meta)
d_write = seriesData_safeWrite(str_fileName, d_meta)
b_status = d_write['status']
if d_write['status']:
d_ret = value
Expand Down

0 comments on commit 2ac17b6

Please sign in to comment.