Python fcntl Library flock and lockf functions

Python fcntl Library flock and lockf functions

Python fcntl Library:

Python provides various functions and commands for using and working with the files in codes. For working with these files we need to define file descriptors.
This Python module performs file control and I/O control on file descriptors. It is an interface to the fcntl() and ioctl() Unix routines. All the functions used in this module take a file descriptor 'fd' as their first argument to know which file they need to handle in the code.

The next two functions in this module are described below:

1. fcntl.flock(fd, operation)

This function performs the lock operation on file descriptor fd (file objects providing a fileno() method are accepted as well). 
If the flock() fails, an OSError exception is raised.

2. fcntl.lockf(fd, cmd, len=0, start=0, whence=0)

This is essentially a wrapper around the fcntl() locking calls. fd is the file descriptor (file objects providing a fileno() method are accepted as well) of the file to lock or unlock, and cmd is one of the following values:

i.) LOCK_UN – unlock
(This command unlocks the locked data items/files)

ii.) LOCK_SH – acquire a shared lock
(this is a shared locking command, it is also called the read lock because it is used for reading the data items only. Shared locks supports read integrity and also ensures that the data in not in the position of getting updated by any process during the read-only requests.)

iii.) LOCK_EX – acquire an exclusive lock
(With this command the data items are locked such that the process which locked the data item/ file can read them as well as write and update them also. In Exclusive locks the data item can be locked by one process only.)

When cmd is LOCK_SH or LOCK_EX, it can also be bitwise ORed with LOCK_NB (LOCK_NB means non-blocking) to avoid blocking on lock acquisition. If LOCK_NB is used and the lock cannot be acquired, an OSError will be raised and the exception will have an errno attribute set to EACCES or EAGAIN (depending on the operating system). On at least some systems, LOCK_EX can only be used if the file descriptor refers to a file opened for writing.

'len' is the number of bytes to lock.
'start' is the byte offset at which the lock starts, relative to 'whence'.
'whence' is as with, specifically take below mentioned numbers:

0 – relative to the start of the file (os.SEEK_SET)

1 – relative to the current buffer position (os.SEEK_CUR)

2 – relative to the end of the file (os.SEEK_END)

The default for start is 0, which means to start at the beginning of the file. The default for len is 0 which means to lock to the end of the file. The default for whence is also 0.

More Articles of Arkaja Sharan:

Name Views Likes
Python codecs Library Error Handling schemes module functions 68 0
Python codecs Library Error Handler register_error and lookup_error functions 64 0
Python codecs Library Error Handlers 67 0
Python codecs Library open and EncodedFile functions 61 0
Python codecs Library iterencode and iterdecode functions 75 0
Python codecs Library register and unregister functions 59 0
Python codecs Library getreader and getwriter functions 72 0
Python codecs Library getincrementalencoder and getincrementaldecoder 54 0
Python codecs Library getencoder and getdecoder functions 60 0
Python Introduction to codecs Library 83 0
Python fcntl Library flock and lockf functions 65 0
Python fcntl Library fcntl and ioctl functions 79 0
Python Resource Library resource usage functions 79 0
Python Resource Library resource usage symbolic constants 63 0
Python Resource Library Resource Limit Functions 75 0
Python resource library resource limit symbolic constants 71 0
Python Introduction to Resource Library 60 0
Python stringprep Library in_table_d1 and in_table_d2 functions 71 0
Python stringprep Library in_table_c8 and in_table_c9 functions 72 0
Python stringprep Library in_table_c5 in_table_c6 and in_table_c7 functions 63 0
Python stringprep Library in_table_c3 and in_table_c4 functions 63 0
Python stringprep library in_table_c21 in_table_c22 and in_table_c21_c22 65 0
Python stringprep library functions in_table_c11 in_table_c12 and in_table_c11_c12 68 0
Python Introduction to stringprep Library 67 0
Python unicodedata library is_normalized unidata_version and ucd_3_2_0 68 0
Python Unicodedata Library functions normalize and decomposition 117 0
Python Unicodedata Library functions east_asian_width and mirrored 73 1
Python Unicodedata Library category bidirectional and combining functions 102 0
Introduction to Unicodedata library lookup and name functions 69 0
Unicode Library decimal digit and numeric functions 66 0
Introduction to Unicode Data library 0 0