`

File Locking Using a Cross-Platform API(python 跨平台文件锁实现)

 
阅读更多

 

File Locking Using a Cross-Platform API

获得平台无关的文件锁

Credit: Jonathan Feinberg, John Nielsen

 转自:http://blog.csdn.net/fdayok/article/details/5263061

 

问题 Problem

 

You need to lock files in a cross-platform way between NT and Posix, but the Python standard library offers only platform-specific ways to lock files.

Python标准库未提供锁定文件的平台无关的方法,需要自己编写这样的平台无关(在NT和Posix之间)的锁定文件的代码。

 

解决 Solution

 

When the Python standard library itself doesn't offer a cross-platform solution, it's often possible to implement one ourselves:

当Python标准库未提供某功能的平台无关的方法是,经常可以自己实现:

import os                                                                    
                                                                             
# needs win32all to work on Windows                                          
if os.name == 'nt':                                                          
    import win32con, win32file, pywintypes                                   
    LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK                               
    LOCK_SH = 0 # the default                                                
    LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY                             
    _ _overlapped = pywintypes.OVERLAPPED(  )                                
                                                                             
    def lock(file, flags):                                                   
        hfile = win32file._get_osfhandle(file.fileno(  ))                    
        win32file.LockFileEx(hfile, flags, 0, 0xffff0000, _ _overlapped)     
                                                                             
    def unlock(file):                                                        
        hfile = win32file._get_osfhandle(file.fileno(  ))                    
        win32file.UnlockFileEx(hfile, 0, 0xffff0000, _ _overlapped)          
elif os.name == 'posix':                                                     
    from fcntl import LOCK_EX, LOCK_SH, LOCK_NB                              
                                                                             
    def lock(file, flags):                                                   
        fcntl.flock(file.fileno(  ), flags)                                  
                                                                             
    def unlock(file):                                                        
        fcntl.flock(file.fileno(  ), fcntl.LOCK_UN)                          
else:                                                                        
    raise RuntimeError("PortaLocker only defined for nt and posix platforms")
 

讨论 Discussion

 

If you have multiple programs or threads that may want to access a shared file, it's wise to ensure that accesses are synchronized, so that two processes don't try to modify the file contents at the same time. Failure to do so could corrupt the entire file in some cases.

如果有多个程序或线程需要访问一个共享文件,明智的处理是保证访问同步化, 这样2个进程不会同时修改文件. 否则如果没有同步处理,在一些情况下,整个程序可能会崩溃。

This recipe supplies two functions, lock and unlock, that request and release locks on a file, respectively. Using the portalocker.py module is a simple matter of calling the lock function and passing in the file and an argument specifying the kind of lock that is desired:

脚本提供了两个函数: lock 和unlock,分别对文件加锁和释放锁。使用portalocker.py模块很简单,仅需要调用lock函数,传递文件参数和欲加的锁类型参数:

LOCK_SH A shared lock (the default value). This denies all processes write access to the file, including the process that first locks the file. All processes can read the locked file.

共享锁(默认值)。所有进程没有写访问权限,即使是加锁进程也没有。所有进程有读访问权限。

LOCK_EX An exclusive lock. This denies all other processes both read and write access to the file.

排他锁。 除加锁进程外其他进程没有对已加锁文件读写访问权限。

LOCK_NB A nonblocking lock. If this value is specified, the function returns immediately if it is unable to acquire the requested lock. Otherwise, it waits. LOCK_NB can be ORed with either LOCK_SH or LOCK_EX.

非阻塞锁。 如果指定此参数,函数不能获得文件锁就立即返回,否则,函数会等待获得文件锁。LOCK_NB可以同LOCK_SH或LOCK_NB进行or运算。

For example:

例如:

import portalocker                         
file = open("somefile", "r+")              
portalocker.lock(file, portalocker.LOCK_EX)
 

The implementation of the lock and unlock functions is entirely different on Unix-like systems (where they can rely on functionality made available by the standard fcntl module) and on Windows systems (where they must use the win32file module, part of the very popular win32all package of Windows-specific extensions to Python, authored by Mark Hammond). But the important thing is that, despite the differences in implementation, the functions (and the flags you can pass to the lock function) behave in the same way across platforms. Such cross-platform packaging of differently implemented but equivalent functionality is what lets you write cross-platform applications, which is one of Python's strengths.

lock和unlock地实现在类Unix系统和Windows系统下的完全不同:类Unix系统下依赖于标准模块fcntl提供的功能,Windows系统下使用了非常流行的由Mark Hammond实现的Windows下特定扩展中的win32all模块提供的功能。虽然不同系统下的实现不同,但重要的是,不同平台下文件加解锁的行为是相同的。这样,将不同的实现但相同的行为按照平台无关的方式用模块封装起来,是Python的一大优势:可以编写平台无关的应用。

When you write a cross-platform program, it's nice if the functionality that your program uses is, in turn, encapsulated in a cross-platform way. For file locking in particular, this is helpful to Perl users, who are used to an essentially transparent lock system call across platforms. More generally, if os.name== just does not belong in application-level code. It should ideally always be in the standard library or an application-independent module, as it is here.

当编写跨平台的程序时,如果使用的功能函数已经是经过平台无关的封装处理的,那样会很舒服。特别的,对于文件加锁,perl程序员就不用费心了: Perl提供了透明的跨平台的文件加锁功能。 更一般的,如果os.name不应该出现在应用级别的代码中 ,那么最好它们应该位于Python标准库中 或者在应用无关的模块中(就像 这里的脚本一样).

分享到:
评论

相关推荐

    locking-selftest-spin.rar_elf

    总结起来,"locking-selftest-spin.rar_elf"的主题涵盖了32位ELF兼容性支持的实现,主要通过"compat_binfmt_elf.c"实现,以及自旋锁的测试,由"locking-selftest-spin.c"体现。这一主题对于理解Linux内核如何处理...

    locking-selftest-spin-softirq.rar_Extras

    【标题】"locking-selftest-spin-softirq.rar_Extras" 提供的是Linux系统中的一个测试程序,主要关注锁机制、自旋锁(spinlock)以及软中断(softirq)的相关内容。这个压缩包包含了用于测试的源代码文件,是Asus ...

    locking-selftest-spin-softirq.rar_decide

    标题“locking-selftest-spin-softirq.rar_decide”和描述中的“provide all the vectors, so that EQ creation response can decide which one to use”涉及到的是Linux内核中的锁机制、中断处理和设备驱动编程的...

    Python Cookbook, 2nd Edition

    File Locking Using a Cross-Platform API Recipe 2.29. Versioning Filenames Recipe 2.30. Calculating CRC-64 Cyclic Redundancy Checks Chapter 3. Time and Money Introduction Recipe 3.1. ...

    Modulation-free laser frequency offset locking using buffer gas-induced resonance

    We experimentally demonstrate a simple modulation-free scheme for offset locking the frequency of a laser using buffer gas-induced resonance. Our scheme excludes the limitation of low diffraction ...

    locking-selftest-spin-hardirq.rar_partition

    `locking-selftest-spin-hardirq.c` 文件名暗示了一个关于锁机制的自我测试,特别是在硬中断(hardirq)上下文中。在嵌入式系统中,硬中断是系统响应外部硬件事件的快速通道,比如网络数据包接收或定时器超时。在硬...

    Biased Locking in HotSpot - Dave - 2006.pdf

    知识点一:偏向锁(Biased Locking)的起源 偏向锁的概念源自于一篇由Dave、Mark Moir和Bill Scherer共同撰写的论文。作者们指出,传统的Java监控锁(即synchronized锁)在执行CAS操作(比较并交换)时会带来显著的...

    开源项目-dchest-safefile.zip

    6. **跨平台兼容性(Cross-Platform Compatibility)**:由于safefile是一个开源项目,它很可能考虑到了不同操作系统之间的差异,如Unix和Windows系统的文件系统行为,确保在多种环境下都能正常工作。 7. **测试与...

    Python库 | types_filelock-0.1.3-py2.py3-none-any.whl

    1. **文件锁(File Locking)**:在并发环境中,文件锁是防止多个进程或线程同时访问同一文件的重要工具,避免数据冲突和损坏。`filelock`库为Python提供了这一功能,支持Unix、Windows和Python的子进程。 2. **...

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

    总之,Python中利用文件锁实现单例模式是一种有效的Linux系统下解决方案,但需要注意其局限性,如非跨平台兼容性和潜在的锁未释放问题。在设计多进程协作的系统时,理解并正确使用文件锁是至关重要的。

    跨平台进程间通信源码

    6. **文件锁定(File Locking)**:通过flock或fcntl系统调用实现对文件的独占访问。 在跨平台的场景下,通常会采用如Boost.Interprocess、Qt的QProcess、ACE(Adaptive Communication Environment)或自定义的抽象...

    USB Type-C Locking Connector Specification

    - **Locking Connector**:锁定连接器,指具备锁定机制的 USB Type-C 连接器。 #### 二、概述 USB Type-C 锁定连接器是为了解决传统 USB 连接器在某些特定环境下容易松动的问题而设计的一种改进型连接器。通过引入...

    文件锁(Delphi + C)

    标题“文件锁(Delphi + C)”提示我们讨论的重点是如何在Delphi和C中实现文件锁定功能。 文件锁的主要作用是防止多个程序同时访问同一文件,从而避免数据冲突和不一致。在Windows系统中,文件锁通常依赖于操作系统...

    The Q-switched mode-locking of an acousto-optic Nd:YVO4 laser

    根据给定的文件信息,以下是对文章《声光调Q Nd:YVO4激光器的锁模问题探讨》中知识点的详细说明: 1. Q-开关锁模(Q-Switched Mode-Locking, QML)现象: 文章指出,在一个声光调Q Nd:YVO4激光器中观察到了Q-开关...

    High-repetition-rate pulse generation using dual-mode self-injection locking in a Fabry-Perot laser diode

    在高速光通信系统中,高速比特率的光脉冲源非常重要,以满足对通信带宽不断增长的需求,并实现未来的超高速光通信系统。该研究的脉冲生成方法,基于FP-LD的双模自注入锁定技术,是一种创新的技术。 1. 研究背景与...

    locking-selftest-hardirq.rar_The First

    The order of these masks is important. Matching masks will be seen first and the left over flags will end up showing by themselves.

    gradle-dependency-locking-demo

    Gradle 4.8中引入的Dependency Locking演示存储库 这是一个演示存储库,当您实际对其进行引用时,它看起来像这样。 请参阅以了解其实际外观 创建一个模板。 使用start.spring.io创建一个模板。 由于它是用于2.0.2....

    UL 498F:2020 Plugs, Socket-Outlets and Couplers with Arcuate (Locking Type) Contacts -最新完整英文版(88页).pdf

    UL 498F:2020 Plugs, Socket-Outlets and Couplers with Arcuate (Locking Type) Contacts UL 498F 是一项美国安全标准,规定了 plugs、socket-outlets 和 couplers 的安全要求,特别是具有弧形锁定接触点的产品。...

    Femtosecond mode-locking of a fiber laser using a CoSb3-skutterudite-based saturable absorber

    We experimentally demonstrate an ultrafast mode-locker based on a CoSb3 skutterudite topological insulator for femtosecond mode-locking of a fiber laser. The mode-locker was implemented on a side-...

Global site tag (gtag.js) - Google Analytics