转自: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-seh-rt_v6-rev0.7z版本
MinGW-w64安装包,解决MinGW-w64安装过程中的 ERROR RES 及 压缩包已损坏问题。原下载地址: ...
x86_64-13.2.0-release-posix-seh-ucrt-rt_v11-rev1.7z
winlibs-x86_64-posix-seh-gcc-11.3.0-llvm-14.0.3-mingw-w64msvcrt-10.0.0-r3.zip
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。
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=*...
mingw 可以用于在windows上配合vscode使用,直接解压,配置好环境变量就可以使用了,将解压后的bin路径粘贴到path后面 windows版本的gcc g++ 使用参考:https://carson.blog.csdn.net/article/details/125514602
MinGW 64位 最新版离线安装包 gcc version 13.1.0
MinGW(x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z)
【mingw官网】https://sourceforge.net/projects/mingw-w64/files/ 【版本】MinGW-W64 GCC-8.1.0版本 【文件】x86_64-posix-seh
mingw x86_64-7.3.0-release-posix-seh-rt_v5-rev0离线完整安装包,无需安装,配置环境变量后即可用。
MingW-x86_64-8.1.0-release-posix-seh-rt_v6-rev0
../configure --prefix=/usr/local/gcc-5.4.0 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-4.3.2 --with-mpfr=/usr/local/mpfr-2.4.2 --...
MinGW-W64的64位安装包: X86_64-8.1.0-release-posix-seh-rt_v6_rev0.7z 支持GCC8.1.0, 解压后即可使用。 利用下载工具下载的也是此压缩包,然后解压缩而已。 方便没梯子的!!!!
MinGW-W64的32位安装包:i686-8.1.0-release-posix-dwarf-rt_v6-rev0.7z ,支持GCC8.1.0,线程模型为posix,异常处理模型为dwarf, 解压后即可使用。 mingw-w64 版本选择方法: 32位系统选择i686, 64位系统选择x86_...
x86_64-12.2.0-release-posix-seh-msvcrt-rt_v10-rev2 windows10编译 gcc C语言,C++
C/C++轻量编辑器 vs code依赖环境 mingw 使用方法 1、将该压缩包拷贝至任意目录下,用解压缩文件解压 2、解压后,复制bin目录路径,新增系统path环境变量 3、验证 同时按下 win+r 输入CMD打开命令行 输入 g++ ...
mingw-w64离线包,解压就可以使用,无需安装,该资源只为方便下载,官网 http://www.mingw-w64.org/doku.php 下载太慢了,如果可以连接官网,最好在官网下载。
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。
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