-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathfile_lock.py
54 lines (46 loc) · 1.4 KB
/
file_lock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
# Leo Zeng
'''
这部分代码多来自网上,linux部分未验证
'''
import os
import random
import utils
class CFileLock(object):
def __init__(self, filename):
self.filename = filename
@utils.RetryFunc(iTimes=3, iSec=random.randint(1, 5))
def Write(self, msg):
"""进程比较多的情况下数据容易出现问题,目前还没想到较好的办法"""
self.handle = open(self.filename, 'a+')
self.acquire()
self.handle.write(msg + "\n")
self.release()
self.handle.close()
def acquire(self):
# 给文件上锁
if os.name == 'nt':
import win32con
import win32file
import pywintypes
LOCK_EX = win32con.LOCKFILE_EXCLUSIVE_LOCK
overlapped = pywintypes.OVERLAPPED()
hfile = win32file._get_osfhandle(self.handle.fileno())
win32file.LockFileEx(hfile, LOCK_EX, 0, -0x10000, overlapped)
elif os.name == 'posix':
import fcntl
LOCK_EX = fcntl.LOCK_EX
fcntl.flock(self.handle, LOCK_EX)
def release(self):
# 文件解锁
if os.name == 'nt':
import win32file
import pywintypes
overlapped = pywintypes.OVERLAPPED()
hfile = win32file._get_osfhandle(self.handle.fileno())
win32file.UnlockFileEx(hfile, 0, -0x10000, overlapped)
elif os.name == 'posix':
import fcntl
fcntl.flock(self.handle, fcntl.LOCK_UN)
def WriteLogfile(sLogFile, sMsg):
CFileLock(sLogFile).Write(sMsg)