`

portalocker - Cross-platform (posix/nt) API for flock-style file locking

 
阅读更多

转自:http://code.activestate.com/recipes/65203/

 

portalocker - Cross-platform (posix/nt) API for flock-style file locking. (Python recipe)

 

 

## {{{ http://code.activestate.com/recipes/65203/ (r7)
# portalocker.py - Cross-platform (posix/nt) API for flock-style file locking.
#                  Requires python 1.5.2 or better.
"""Cross-platform (posix/nt) API for flock-style file locking.

Synopsis:

   import portalocker
   file = open("somefile", "r+")
   portalocker.lock(file, portalocker.LOCK_EX)
   file.seek(12)
   file.write("foo")
   file.close()

If you know what you're doing, you may choose to

   portalocker.unlock(file)

before closing the file, but why?

Methods:

   lock( file, flags )
   unlock( file )

Constants:

   LOCK_EX
   LOCK_SH
   LOCK_NB

Exceptions:

    LockException

Notes:

For the 'nt' platform, this module requires the Python Extensions for Windows.
Be aware that this may not work as expected on Windows 95/98/ME.

History:

I learned the win32 technique for locking files from sample code
provided by John Nielsen <nielsenjf@my-deja.com> in the documentation
that accompanies the win32 modules.

Author: Jonathan Feinberg <jdf@pobox.com>,
        Lowell Alleman <lalleman@mfps.com>
Version: $Id: portalocker.py 5474 2008-05-16 20:53:50Z lowell $

"""


__all__ = [
    "lock",
    "unlock",
    "LOCK_EX",
    "LOCK_SH",
    "LOCK_NB",
    "LockException",
]

import os

class LockException(Exception):
    # Error codes:
    LOCK_FAILED = 1

if os.name == 'nt':
    import win32con
    import win32file
    import pywintypes
    LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
    LOCK_SH = 0 # the default
    LOCK_NB = win32con.LOCKFILE_FAIL_IMMEDIATELY
    # is there any reason not to reuse the following structure?
    __overlapped = pywintypes.OVERLAPPED()
elif os.name == 'posix':
    import fcntl
    LOCK_EX = fcntl.LOCK_EX
    LOCK_SH = fcntl.LOCK_SH
    LOCK_NB = fcntl.LOCK_NB
else:
    raise RuntimeError, "PortaLocker only defined for nt and posix platforms"

if os.name == 'nt':
    def lock(file, flags):
        hfile = win32file._get_osfhandle(file.fileno())
        try:
            win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped)
        except pywintypes.error, exc_value:
            # error: (33, 'LockFileEx', 'The process cannot access the file because another process has locked a portion of the file.')
            if exc_value[0] == 33:
                raise LockException(LockException.LOCK_FAILED, exc_value[2])
            else:
                # Q:  Are there exceptions/codes we should be dealing with here?
                raise
    
    def unlock(file):
        hfile = win32file._get_osfhandle(file.fileno())
        try:
            win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped)
        except pywintypes.error, exc_value:
            if exc_value[0] == 158:
                # error: (158, 'UnlockFileEx', 'The segment is already unlocked.')
                # To match the 'posix' implementation, silently ignore this error
                pass
            else:
                # Q:  Are there exceptions/codes we should be dealing with here?
                raise

elif os.name == 'posix':
    def lock(file, flags):
        try:
            fcntl.flock(file.fileno(), flags)
        except IOError, exc_value:
            #  IOError: [Errno 11] Resource temporarily unavailable
            if exc_value[0] == 11:
                raise LockException(LockException.LOCK_FAILED, exc_value[1])
            else:
                raise
    
    def unlock(file):
        fcntl.flock(file.fileno(), fcntl.LOCK_UN)



if __name__ == '__main__':
    from time import time, strftime, localtime
    import sys
    import portalocker

    log = open('log.txt', "a+")
    portalocker.lock(log, portalocker.LOCK_EX)

    timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time()))
    log.write( timestamp )

    print "Wrote lines. Hit enter to release lock."
    dummy = sys.stdin.readline()

    log.close()
## end of http://code.activestate.com/recipes/65203/ }}}
分享到:
评论

相关推荐

    MinGW(x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z)

    这个压缩包“x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z”包含了MinGW针对x86_64架构的64位版本,版本号为8.1.0。让我们深入了解MinGW以及这个特定版本包含的内容。 首先,GCC是GNU项目的核心组成部分,它支持...

    mingw-x86-64-8.1.0-release-posix-seh-rt-v6-rev

    MingW-x86_64-8.1.0-release-posix-seh-rt-v6-rev 是一个针对Windows平台的MinGW-w64编译器套件的特定版本,用于支持64位架构的C++开发。这个版本是8.1.0,基于POSIX标准并且使用SEH(结构化异常处理)来处理异常,...

    x86_64-4.9.1-release-posix-seh-rt_v3-rev0.7z

    MinGW-w64安装包,解决MinGW-w64安装过程中的 ERROR RES 及 压缩包已损坏问题。原下载地址: ...

    x86-64-13.2.0-release-posix-seh-rev1.7z

    标题 "x86-64-13.2.0-release-posix-seh-rev1.7z" 暗示这是一个针对x86_64架构的软件编译环境,其中包含了 Mingw64、GCC 和 G++ 的组件。这个压缩包很可能是用于在Windows操作系统上构建64位应用程序的工具集。让...

    arm-2014.05-29-arm-none-linux-gnueabi

    COLLECT_LTO_WRAPPER=d:/eclipse/cross/arm-2014.05-29/bin/../libexec/gcc/arm-none-linux-gnueabi/4.8.3/lto-wrapper.exe Target: arm-none-linux-gnueabi Configured with: /scratch/maciej/arm-linux-2014.05-...

    x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1.7z

    标题 "x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1.7z" 暗示这是一个针对x86_64架构的编译工具链的打包文件,主要用于Windows平台。这种工具链通常包括编译器、链接器以及其他构建C++应用程序所需的组件。...

    mingw-w64_x86_64-7.1.0-release-posix-seh-rt_v5-rev0

    MingW-w64_x86_64-7.1.0-release-posix-seh-rt_v5-rev0是一个针对64位Windows操作系统编译环境的软件包,主要用于开发跨平台的应用程序,特别是C和C++编程。MingW-w64是MinGW(Minimalist GNU for Windows)的扩展,...

    winlibs-x86_64-posix-seh-gcc-11.3.0-llvm-14.0.3-mingw-w64msvcrt-10.0.0-r3.zip

    winlibs-x86_64-posix-seh-gcc-11.3.0-llvm-14.0.3-mingw-w64msvcrt-10.0.0-r3.zip

    arm-linux-gcc-4.3.2(交叉编译器)

    arm-linux-gcc交叉编译器,版本...threads=posix --enable-symvers=gnu --enable-__cxa_atexit --enable-languages=c,c++ --enable-shared --enable-c99 --enable-long-long Thread model: posix gcc version 3.4.1。

    MinGW64_x86_64-12.2.0-release-posix-seh-rt_v10-rev0

    windows下64位gcc编译器v12.2 Release for GCC-12.2.0 with MinGW-W64 runtime version 10 添加编译批处理g++.bat和g++版的EasyX。 来源地址:https://github.com/niXman/mingw-builds-binaries/releases

    x86_64-4.8.5-release-posix-sjlj-rt_v4-rev0.7z

    args : --mode=gcc-4.8.5 --buildroot=/c/mingw485 --rt-version=v4 --rev=0 --bootstrap --jobs=2 --threads=posix --exceptions=sjlj --arch=x86_64 --bin-compress --mingw-upload --sf-user=nixmann --sf-pass=*...

    x86-64-14.2.0-release-posix-seh-msvcrt-rt-v12-rev0.7z

    C语言编译器MinGW x86_64-14.2.0-release-posix-seh-msvcrt-rt_v12-rev0.7z

    MinGW 64位 最新版离线安装包:x86-64-13.1.0-release-posix-seh-ucrt-rt-v11-r

    标题中提到的"MinGW 64位 最新版离线安装包:x86-64-13.1.0-release-posix-seh-ucrt-rt-v11-r",意味着这个压缩包包含了MinGW的最新64位版本,其核心组件GCC已经更新到了13.1.0版本。这里的"x86-64"表示适用于64位...

    winlibs-x86-64-posix-seh-gcc-13.2.0-llvm-16.0.6-mingw-w64msvcrt

    标题“winlibs-x86-64-posix-seh-gcc-13.2.0-llvm-16.0.6-mingw-w64msvcrt”指的是一个专为Windows平台设计的C和C++编译工具链。这个工具链集成了GCC (GNU Compiler Collection) 13.2.0版本和LLVM 16.0.6编译器,...

    mingw 原文件名x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z

    mingw 可以用于在windows上配合vscode使用,直接解压,配置好环境变量就可以使用了,将解压后的bin路径粘贴到path后面 windows版本的gcc g++ 使用参考:https://carson.blog.csdn.net/article/details/125514602

    i686-5.2.0-release-posix-dwarf-rt_v4-rev1.7z

    "i686-5.2.0-release-posix-dwarf-rt_v4-rev1.7z" 文件名表明这是MingW的特定构建,包含了针对i686处理器的POSIX兼容性和调试信息(DWARF格式),以及运行时库(RT)的第4版修订1。 在描述中提到“支持编译Qt4.8”...

    x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev1.7z

    标题 "x86_64-13.2.0-release-posix-seh-msvcrt-rt_v11-rev1.7z" 暗示这是一个针对 x86_64 架构的 MingW (Minimalist GNU for Windows) 发行版,版本号为 13.2.0,并且是 release 版本。"posix-seh" 表明它支持 ...

    x86_64-8.1.0-release-posix-seh-rt_v6-rev0.zip

    这个压缩包"**x86_64-8.1.0-release-posix-seh-rt_v6-rev0.zip**"是Mingw-w64的一个特定版本,适用于64位Windows系统,即x86_64架构。这个版本是8.1.0,表明它基于GCC 8.1.0,这是一个相当新的版本,包含了对C、C++...

    X86_64-8.1.0-release-posix-seh-rt_v6_rev0.7z

    MinGW-W64的64位安装包: X86_64-8.1.0-release-posix-seh-rt_v6_rev0.7z 支持GCC8.1.0, 解压后即可使用。 利用下载工具下载的也是此压缩包,然后解压缩而已。 方便没梯子的!!!!

    x86_64-5.4.0-release-posix-seh-rt_v5-rev0

    标题 "x86_64-5.4.0-release-posix-seh-rt_v5-rev0" 和描述 "mingw-w64, x86_64-5.4.0, x86_64-5.4.0-release-posix-seh-rt_v5-rev0" 提供了关于一个特定的编译器版本和环境的信息。这个环境是为Windows平台上的64位...

Global site tag (gtag.js) - Google Analytics