ripple-python-common/files/fileLocks.py

33 lines
652 B
Python
Raw Normal View History

2018-12-09 19:58:56 +00:00
import threading
class fileLocks:
def __init__(self):
# Dictionary containing threading.Lock s
self.locks = {}
def lockFile(self, fileName):
"""
Set a file as locked.
:param fileName: file name
:return:
"""
if fileName in self.locks:
# Acquire existing lock
self.locks[fileName].acquire()
else:
# Create new lock and acquire it
self.locks[fileName] = threading.Lock()
self.locks[fileName].acquire()
def unlockFile(self, fileName):
"""
Unlock a previously locked file
:param fileName: file name
:return:
"""
if fileName in self.locks:
# Release lock if it exists
self.locks[fileName].release()