python 使用跨平台文件锁

#encoding=utf-8

print '中国'

#使用跨平台文件锁

import os

if os.name == 'nt':
import win32con,win32file,pywintypes
LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
LOCK_SH = 0
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 lock(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("not supported")
---------------------
作者:houyj1986
来源:CSDN
原文:https://blog.csdn.net/houyj1986/article/details/21419911
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2018-11-05 16:12  decode126  阅读(1356)  评论(0编辑  收藏  举报