Skip to content

Instantly share code, notes, and snippets.



Created Dec 17, 2017
What would you like to do?
A custom-file-lock wrapper for fcntl in python
#coding: utf-8
import os
import time
import fcntl
import contextlib
import logging
def open_file(filename, pattern = 'r', timeout = 5):
""" 以加锁的方式读取文件,尝试timeout * 10 次,每次sleep 1秒钟.
Keyword arguments:
filename -- file to open
pattern: 文件打开方式,'r' or 'w'
timeout: try times (default 5)
fd = open(filename, pattern)
if pattern == 'r' or pattern == 'rb':
lock_type = fcntl.LOCK_SH
elif pattern == 'w' or pattern == 'wb':
lock_type = fcntl.LOCK_EX
raise Exception('Wrong pattern!')
# try to acquire the lock
get_lock = False
for i in xrange(int(timeout * 10)):
fcntl.flock(fd, lock_type | fcntl.LOCK_NB)
get_lock = True
logging.debug('Acquire lock of file %s success!' % filename)
except IOError:
if not get_lock:
# close file
raise IOError('Unable to acquire lock for %s' % filename)
yield fd
# unlock
fcntl.flock(fd, fcntl.LOCK_UN)
logging.debug('Release lock of file %s success!' % filename)
# close file
if __name__ == '__main__':
with open_file('./test', 'r') as fin:
for line in fin:
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.