python中正确的多进程安全文件锁定

时间:2015-02-06 16:05:55

标签: python multiprocessing

我正在尝试为小型应用程序实现线程和多进程安全磁盘缓存。它只需要在现代Linux发行版上运行,不需要与Windows或OS X兼容。

以下是代码:

class LockedOpen(object):

    READ = 0
    WRITE = 1

    def __init__(self, filename, mode=READ, block=True):
        self._filename = filename
        if mode == LockedOpen.READ:
            self._open_mode = os.O_RDONLY
            self._lock_op = fcntl.LOCK_SH
            self._open_mode_str = 'rb'
        else:
            self._open_mode = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
            self._lock_op = fcntl.LOCK_EX
            self._open_mode_str = 'wb'

        if not block:
            self._lock_op = self._lock_op | fcntl.LOCK_NB

    def __enter__(self,):
        self._fd = os.open(self._filename, self._open_mode)
        fcntl.flock(self._fd, self._lock_op)
        self._file_obj = os.fdopen(self._fd, self._open_mode_str)
        return self._file_obj

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._file_obj.flush()
        os.fdatasync(self._fd)
        fcntl.flock(self._fd, fcntl.LOCK_UN)
        self._file_obj.close()


class DiskCache(object):
    def __init__(self, dirname):
        self._dir = dirname

    def _filename_from_key(self, key):
        return os.path.join(self._dir, key)

    def put(self, key, value):
        with LockedOpen(self._filename_from_key(key), LockedOpen.WRITE) as f:
            f.write(value)

    def get(self, key):
        with LockedOpen(self._filename_from_key(key)) as f:
            return f.read()

    def delete(self, key):
        os.unlink(self._filename_from_key(key))

问题是,我这样做了吗?我应该知道这些代码是否有任何错误/警告/含义?任何关于表现的建议也是受欢迎的。

感谢〜

0 个答案:

没有答案