forked from p15670423/monkey
Island: Use read/write lock in FileRepositoryLockingDecorator
This commit is contained in:
parent
48cc6d6dd2
commit
a8b54f69f9
|
@ -1,30 +1,27 @@
|
|||
from multiprocessing import Lock
|
||||
from typing import BinaryIO
|
||||
|
||||
from . import IFileRepository
|
||||
from readerwriterlock import rwlock
|
||||
|
||||
# TODO: Use a read-write lock. See:
|
||||
# - https://pypi.org/project/readerwriterlock/
|
||||
# - https://www.oreilly.com/library/view/python-cookbook/0596001673/ch06s04.html
|
||||
from . import IFileRepository
|
||||
|
||||
|
||||
class FileRepositoryLockingDecorator(IFileRepository):
|
||||
def __init__(self, file_repository: IFileRepository):
|
||||
self._file_repository = file_repository
|
||||
self._lock = Lock()
|
||||
self._rwlock = rwlock.RWLockFair()
|
||||
|
||||
def save_file(self, unsafe_file_name: str, file_contents: BinaryIO):
|
||||
with self._lock:
|
||||
with self._rwlock.gen_wlock():
|
||||
return self._file_repository.save_file(unsafe_file_name, file_contents)
|
||||
|
||||
def open_file(self, unsafe_file_name: str) -> BinaryIO:
|
||||
with self._lock:
|
||||
with self._rwlock.gen_rlock():
|
||||
return self._file_repository.open_file(unsafe_file_name)
|
||||
|
||||
def delete_file(self, unsafe_file_name: str):
|
||||
with self._lock:
|
||||
with self._rwlock.gen_wlock():
|
||||
return self._file_repository.delete_file(unsafe_file_name)
|
||||
|
||||
def delete_all_files(self):
|
||||
with self._lock:
|
||||
with self._rwlock.gen_wlock():
|
||||
return self._file_repository.delete_all_files()
|
||||
|
|
Loading…
Reference in New Issue