File lock fcntl

A, python lock files

When we write python application when it comes to multiple processes to write (or read) in the case of the same file, if several processes simultaneously have to write this document, the contents of the file will become very confusing, this time the file lock comes in handy.

python lock files, you can ensure that only one process file write, are currently using this library fcntl, it actually provides an ioctl interface on Unix, flock and fcntl function. python by calling fcntl.flock () function of file locking.

 

fcntl This module is built-in Python, but not Windows, you can manually download fcntl.py file and save it under the python Lib directory.

 

Second, the type of lock

  • LOCK_SH: pledged to create a shared lock, at any time, a shared lock on a file can be owned by multiple processes
  • LOCK_EX: represents the creation of an exclusive lock, at any time, a file has an exclusive lock can only be a process
  • LOCK_UN: means to delete lock created by the process (unlocked)
  • LOCK_MAND: It is mainly used in shared mode locks mandatory, it can combine with LOCK_READ or LOCK_WRITE use, thereby indicating whether to allow concurrent read or write operation concurrent (although the manual page flock () is not covered LOCK_MAND, but read the kernel source code will find that this has been achieved in the kernel)
  • LOCK_NB: If specified, the function returns a file lock can not be obtained immediately. Otherwise, the function will wait to obtain a lock file. LOCK_NB may be the same or LOCK_SH bit or LOCK_EX (|) arithmetic operation. 

For example: a file is set up exclusive lock, if the lock has been acquired a process, then the other process requests acquire the lock time will be blocked.
If you do not want to clog the process in the case of those who did not get the exclusive lock, can be used in conjunction with LOCK_NB, then the system will not block the process. Namely: fcnt.flock (f, fcntl.LOCK_EX | fcntl.LOCK_NB )

 

Third, pay attention to points

  1. For file close () operation will fail file locks;
  2. Similarly, after the end of the process file lock failure;
  3. flock () is the LOCK_EX "advised lock", the kernel does not enforce check the status of the lock, you need to explicitly check local file operations can take effect in the code.
  4. Prior to the lock file, make sure the file is opened with the appropriate access mode, for example:
    To add a shared lock file, be sure to first press the read mode to open the file;
    To add files to an exclusive lock, the first open the corresponding file according to a write mode;
    if you want to add other kinds of locks, it is necessary to open the press-write mode.

 

Fourth, example

import fcntl


class Lock(object):
    def __init__(self, file_name):
        self.file_name = file_name
        self.handle = open(file_name, 'w+')

    def lock(self):
        fcntl.flock(self.handle, fcntl.LOCK_EX | fcntl.LOCK_NB)

    def unlock(self):
        fcntl.flock(self.handle, fcntl.LOCK_UN)

    def __del__(self):
        try:
            self.handle.close()
        except:
            pass

 

Guess you like

Origin www.cnblogs.com/Zzbj/p/11068131.html