Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import errno
- import fcntl
- def open_file_write_exclusive(file_name):
- """
- If file is available - returns file object.
- If file is locked - returns None.
- If error occured - throws error.
- Truncates opened file.
- """
- try:
- fd = os.open(file_name, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
- except OSError as e:
- if e.errno == errno.EEXIST:
- fd = os.open(file_name, os.O_WRONLY)
- else:
- raise
- fl = os.fdopen(fd, 'w')
- try:
- fcntl.flock(fl, fcntl.LOCK_EX | fcntl.LOCK_NB)
- except IOError as e:
- if e.errno == errno.EAGAIN:
- return None
- else:
- raise
- fl.truncate(0)
- return fl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement