如何在Linux中的python进程之间锁定目录? [英] How to lock a directory between python processes in linux?

查看:183
本文介绍了如何在Linux中的python进程之间锁定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个(或多个)正在运行的python进程,并希望创建类似于排除互斥锁获取共享资源.在这种情况下,共享资源"是一个目录.我如何最容易/标准/等地实现互斥锁?每个进程都同意检查的隐藏.lock文件,如果存在,则将其PID添加为新行,然后在他们有权访问该文件时弹出其PID?

I have two (or more) python processes running and want to create a concept similar to an exclusion mutex for a shared resource. The 'shared resource' in this case is a directory. How might I most easily/standardly/etc implement a mutex? A hidden .lock file that each process agrees to check and, if exists, appends their PID as a new row and then pops their PID when they have access to the file?

我基本上只想清除目录,并确保在清除目录时没有其他进程尝试读取或写入该目录.

I basically just want to clear a directory and make sure no other process tries to read or write to it while I'm clearing it.

是否有标准的linux方式来做到这一点?也许我可以用python的shell行执行某些事情?

Is there a standard linux way of doing this? Maybe something I can just execute with a shell line from python?

推荐答案

Linux

Linux中有两种标准的锁定类型:强制锁定(特定于Linux).

There are two standard types of locking in Linux: advisory locking (specified in POSIX) and mandatory locking (Linux-specific).

但是,它们两者都只能应用于文件,而不能应用于目录.所以是的,您需要一个锁定文件.它假定所有用户在访问目录之前都应该了解该锁文件并获取该锁.因此,强制锁定在这里无济于事,您需要咨询性锁定.

However, both of them may be applied only on files, but not directories. So yes, you need a lock-file. It assumes that all users should know about the lock-file and acquire the lock before accessing directory. Hence, mandatory locking will not help here, and you need advisory locking.

Linux中有三种咨询文件锁:

There are three kinds of advisory file locks in Linux:

  • flock(2) (在POSIX中指定);
  • POSIX记录锁定,请参见 fcntl(2) 以及 lockf(3) 包装器(均在POSIX中指定);
  • 打开文件描述锁,请参见 fcntl(2) (特定于Linux,在最近的内核中可用).

Python

在Python中,可以通过flock(),lockf()fcntl()函数. > fcntl 模块.还有 flock 模块,它为fcntl.flock函数添加了上下文管理器支持.

In Python, flock(), lockf() and fcntl() functions are available through fcntl module. There is also flock module that adds context-manager support to fcntl.flock function.

这里是一个例子:

import flock

with open('/my/dir/lockfile', 'w') as fp:
    with flock.Flock(fp, flock.LOCK_EX) as lock:
        pass # exclusive lock is acquired here


PS.

使用这种方法,如果它不知道您的锁定文件,则不能阻止随机进程访问您的目录.可能可以使用 FUSE 实现文件系统,该系统将支持强制性目录锁定,但是我我不知道这样的实现.

With this approach, you can not prevent a random process from accessing your directory, if it don't know about your lock-file. It is probably possible to implement a filesystem using FUSE that will support mandatory directory locks, but I'm not aware of such implementations.

这篇关于如何在Linux中的python进程之间锁定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆