`
xiaoyu966
  • 浏览: 254345 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

python之跨进程锁的实现---fcntl

阅读更多

============================================================================

原创作品,允许转载。转载时请务必以超链接形式标明原始出处、以及本声明。

请注明转自:http://yunjianfei.iteye.com/blog/

============================================================================

 

跨进程锁的实现方式中,基于文件锁的方式相对来说好一点。以下贴出一个简单的代码:

 

import os
import fcntl

class Lock: 
    def __init__(self, filename):
        self.filename = filename
        # This will create it if it does not exist already
        self.handle = open(filename, 'w')
    
    # Bitwise OR fcntl.LOCK_NB if you need a non-blocking lock 
    def acquire(self):
        fcntl.flock(self.handle, fcntl.LOCK_EX)
        
    def release(self):
        fcntl.flock(self.handle, fcntl.LOCK_UN)
        
    def __del__(self):
        self.handle.close()

# Usage
try:
    lock = Lock("/tmp/lock_name.tmp")
    lock.acquire()
    # Do important stuff that needs to be synchronized

finally: 
    lock.release()

 可以同时运行多份该程序来进行试验。

 

这种方式的锁有以下特点:

1. 锁文件只在第一次调用的时候创建。

2. 锁是通过kernel来控制的,不消耗磁盘IO

3. 这种方式的锁只对同一个OS中的进程有效。跨服务器、OS是无效的,这时候需要选用分布式锁,比如Elock  http://dustin.sallings.org/elock/

 

1
0
分享到:
评论

相关推荐

    fcntl模块,python

    将资源fcntl.py下载下来,放到python的安装目录下的\Lib目录内即可

    Python使用文件锁实现进程间同步功能【基于fcntl模块】

    本文实例讲述了Python使用文件锁实现进程间同步功能。分享给大家供大家参考,具体如下: 简介 在实际应用中,会出现这种应用场景:希望shell下执行的脚本对某些竞争资源提供保护,避免出现冲突。本文将通过fcntl模块...

    fcntl.py模块

    python在window上使用python-3.6.5-amd64.exe直接安装时会缺少fcntl.py文件,影响python使用。但是如果使用python-3.6.5-amd64-webinstall.exe安装就不会缺少该文件。解决办法:可以下载该文件放到python安装路径下...

    windows下fcntl.py

    解决windows下缺少fcntl.py文件的问题,下载后放入python安装目录的lib文件就OK

    python使用fcntl模块实现程序加锁功能示例

    本文实例讲述了python使用fcntl模块实现程序加锁功能。分享给大家供大家参考,具体如下: python 中引入给文件加锁的 fcntl模块 import fcntl 打开一个文件 ##当前目录下test文件要先存在,如果不存在会报错。...

    fcntl模块 for windows

    python在window上使用python-3.7.4-amd64.exe直接安装时会缺少fcntl.py文件,影响python使用。但是如果使用python-3.7.4-amd64-webinstall.exe安装就不会缺少该文件。解决办法:可以下载该文件放到python安装路径下...

    fcntl.py文件下载

    fcntl 是posix 类型的系统 比喻 linux的 文件接口部分 对应linux C开发里的fcntl.h python 的linux版本自带的 标准模块 win下是冒得的

    Python简单进程锁代码实例

    先说说线程 在多线程中,为了保证共享资源的正确性,我们常常会用到线程同步技术. 将一些敏感操作变成原子操作,保证同一时刻多个线程...进程里我们通过一些进程间通信方式,可以实现进程间的同步。 最近我遇到的一个情

    python fcntl模块

    解决这个错误问题:ModuleNotFoundError: No module named 'fcntl'

    fcntl压缩文件

    ImportError: No module named fcntl错误解决, 将资源fcntl.py下载下来,放到python的安装目录下的\Lib目录内即可。我的是放到Python\Lib目录下了。

    Linux编程下的fcntl文件锁代码

    这是在Linux应用编程下的fcntl函数的基础实验,具体看博客:http://blog.csdn.net/mybelief321/article/details/8993138

    fcntl模块 win

    python,fcntl模块, 用于windows系统 。

    fcntl.py的压缩文件

    解决python错误问题:importerror no module named fcntl

    一个简单聊天室的两种实现 (fcntl 和 select)

    一个简单聊天室的两种实现 (fcntl 和 select)

    fcntl.py 模块

    ImportError: No module named fcntl错误解决, 将资源fcntl.py下载下来,放到python的安装目录下的\Lib目录内即可。我的是放到D:\Python27\Lib目录下了。

    python 利用文件锁单例执行脚本的方法

    在python中,为了实现这个需求,可以引入fcntl模块对文件加一个排他锁,这样一来,先启动的实例拥有了文件锁,而后启动的实例则因无法获取锁而退出 #coding=utf-8 import fcntl, sys, time, os pidfile = 0 def ...

    python模块安装工具

    windows32位系统下python3.3版本安装模块必须的工具

    fcntl.py 解决no module named fantl的问题

    解决no module named fantl的问题,把文件复制放到你所需要的python path环境的LIb中即可 可自行建fcntl.py文件,敲入如下代码: def fcntl(fd, op, arg=0): return 0 def ioctl(fd, op, arg=0, mutable_flag=True)...

Global site tag (gtag.js) - Google Analytics