Linux上的Python中的系统范围互斥 [英] System-wide mutex in Python on Linux

查看:93
本文介绍了Linux上的Python中的系统范围互斥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么简单的方法可以在Linux上的Python中使用系统范围的互斥锁吗? 全系统"是指互斥锁将由一组Python 进程使用;这与传统互斥锁相反,传统互斥锁由同一进程中的一组线程使用.

Is there any easy way to have a system-wide mutex in Python on Linux? By "system-wide", I mean the mutex will be used by a group of Python processes; this is in contrast to a traditional mutex, which is used by a group of threads within the same process.

我不确定Python的 multiprocessing 包是什么我需要.例如,我可以在两个不同的解释器中执行以下命令:

I'm not sure Python's multiprocessing package is what I need. For example, I can execute the following in two different interpreters:

from multiprocessing import Lock
L = Lock()
L.acquire()

当我在两个单独的解释器中同时执行这些命令时,我希望其中一个挂起.相反,它们都没有挂起.看来他们没有获得相同的互斥体.

When I execute these commands simultaneously in two separate interpreters, I want one of them to hang. Instead, neither hangs; it appears they aren't acquiring the same mutex.

推荐答案

Unix的传统"答案是使用文件锁.您可以使用lockf(3)锁定文件的各个部分,以便其他进程无法编辑该文件.一个非常普遍的滥用是将其用作进程之间的互斥体.等效的python是 fcntl.lockf .

The "traditional" Unix answer is to use file locks. You can use lockf(3) to lock sections of a file so that other processes can't edit it; a very common abuse is to use this as a mutex between processes. The python equivalent is fcntl.lockf.

传统上,您将锁定过程的PID写入锁定文件,以使由于持有锁定而死的进程导致的死锁是可识别和可修复的.

Traditionally you write the PID of the locking process into the lock file, so that deadlocks due to processes dying while holding the lock are identifiable and fixable.

这将为您提供所需的信息,因为您的锁位于全局名称空间(文件系统)中,并且可用于所有进程.这种方法还具有非Python程序可以参与锁定的好处.缺点是您需要一个放置该锁定文件的位置;同样,某些文件系统实际上并没有正确锁定,因此存在无提示无法实现排除的风险.你赢了一些,你输了一些.

This gets you what you want, since your lock is in a global namespace (the filesystem) and accessible to all processes. This approach also has the perk that non-Python programs can participate in your locking. The downside is that you need a place for this lock file to live; also, some filesystems don't actually lock correctly, so there's a risk that it will silently fail to achieve exclusion. You win some, you lose some.

这篇关于Linux上的Python中的系统范围互斥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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