fcntl
— The fcntl
and ioctl
system callsfcntl
和ioctl
系统调用¶
fcntl
and ioctl
system callsThis module performs file control and I/O control on file descriptors. 该模块对文件描述符执行文件控制和I/O控制。It is an interface to the 它是fcntl()
and ioctl()
Unix routines. fcntl()
和ioctl()
Unix例程的接口。For a complete description of these calls, see fcntl(2) and ioctl(2) Unix manual pages.有关这些调用的完整描述,请参阅fcntl(2)和ioctl(2)Unix手册页。
All functions in this module take a file descriptor fd as their first argument. 该模块中的所有函数都将文件描述符fd作为它们的第一个参数。This can be an integer file descriptor, such as returned by 这可以是一个整数文件描述符,例如由sys.stdin.fileno()
, or an io.IOBase
object, such as sys.stdin
itself, which provides a fileno()
that returns a genuine file descriptor.sys.stdin.fileno()
返回,也可以是io.IOBase
对象,例如sys.stdin
本身,它提供了一个返回真正文件描述符的fileno()
。
Changed in version 3.3:版本3.3中更改: Operations in this module used to raise an 此模块中的操作过去会引发IOError
where they now raise an OSError
.IOError
,现在会引发OSError
。
Changed in version 3.8:版本3.8中更改: The fcntl module now contains fcntl模块现在包含F_ADD_SEALS
, F_GET_SEALS
, and F_SEAL_*
constants for sealing of os.memfd_create()
file descriptors.F_ADD_SEALS
、F_GET_SELS
和F_SEAL_*
常量,用于密封os.memfd_create()
文件描述符。
Changed in version 3.9:版本3.9中更改: On macOS, the fcntl module exposes the 在macOS上,fcntl模块公开F_GETPATH
constant, which obtains the path of a file from a file descriptor. F_GETPATH
常量,该常量从文件描述符中获取文件的路径。On Linux(>=3.15), the fcntl module exposes the 在Linux(>=3.15)上,fcntl模块公开F_OFD_GETLK
, F_OFD_SETLK
and F_OFD_SETLKW
constants, which working with open file description locks.F_OFD_GETLK
、F_OFD_SETLK
和F_OFD_SELKW
常量,这些常量与打开的文件描述锁一起工作。
Changed in version 3.10:版本3.10中更改: On Linux >= 2.6.11, the fcntl module exposes the 在Linux>=2.6.11上,fcntl模块公开F_GETPIPE_SZ
and F_SETPIPE_SZ
constants, which allow to check and modify a pipe’s size respectively.F_GETPPIPE_SZ
和F_SETPIPE_SZ
常量,它们允许分别检查和修改管道的大小。
The module defines the following functions:该模块定义了以下功能:
-
fcntl.
fcntl
(fd, cmd, arg=0)¶ Perform the operation cmd on file descriptor fd (file objects providing a对文件描述符fd执行cmd操作(也接受提供fileno()
method are accepted as well).fileno()
方法的文件对象)。The values used for cmd are operating system dependent, and are available as constants in the用于cmd的值依赖于操作系统,并且在fcntl
module, using the same names as used in the relevant C header files.fcntl
模块中可用作常量,使用与相关C头文件中使用的名称相同的名称。The argument arg can either be an integer value, or a参数arg可以是整数值,也可以是bytes
object.bytes
对象。With an integer value, the return value of this function is the integer return value of the C对于整数值,此函数的返回值是Cfcntl()
call.fcntl()
调用的整数返回值。When the argument is bytes it represents a binary structure, e.g. created by当参数是字节时,它表示一个二进制结构,例如由struct.pack()
.struct.pack()
创建。The binary data is copied to a buffer whose address is passed to the C二进制数据被复制到缓冲区,缓冲区的地址被传递给Cfcntl()
call.fcntl()
调用。The return value after a successful call is the contents of the buffer, converted to a成功调用后的返回值是缓冲区的内容,转换为bytes
object.bytes
对象。The length of the returned object will be the same as the length of the arg argument.返回的对象的长度将与arg参数的长度相同。This is limited to 1024 bytes.这被限制为1024个字节。If the information returned in the buffer by the operating system is larger than 1024 bytes, this is most likely to result in a segmentation violation or a more subtle data corruption.如果操作系统在缓冲区中返回的信息大于1024字节,则最有可能导致分段冲突或更微妙的数据损坏。If the如果fcntl()
fails, anOSError
is raised.fcntl()
失败,则会引发一个OSError
。Raises an auditing event使用参数fcntl.fcntl
with argumentsfd
,cmd
,arg
.fd
、cmd
、arg
引发审核事件fcntl.fcntl
。
-
fcntl.
ioctl
(fd, request, arg=0, mutate_flag=True)¶ This function is identical to the此函数与fcntl()
function, except that the argument handling is even more complicated.fcntl()
函数相同,只是参数处理更加复杂。The request parameter is limited to values that can fit in 32-bits.request参数被限制为可以适应32位的值。Additional constants of interest for use as the request argument can be found in the在termios
module, under the same names as used in the relevant C header files.termios
模块中可以找到与相关C头文件中使用的名称相同的、用作request参数的其他感兴趣的常量。The parameter arg can be one of an integer, an object supporting the read-only buffer interface (like参数arg可以是整数、支持只读缓冲区接口(如bytes
) or an object supporting the read-write buffer interface (likebytearray
).bytes
)的对象或支持读写缓冲区接口的对象(如bytearray
)中的一个。In all but the last case, behaviour is as for the除最后一种情况外,其他所有情况下的行为与fcntl()
function.fcntl()
函数相同。If a mutable buffer is passed, then the behaviour is determined by the value of the mutate_flag parameter.如果传递了可变缓冲区,那么行为由mutate_flag参数的值决定。If it is false, the buffer’s mutability is ignored and behaviour is as for a read-only buffer, except that the 1024 byte limit mentioned above is avoided – so long as the buffer you pass is at least as long as what the operating system wants to put there, things should work.如果它是false
,那么缓冲区的可变性将被忽略,其行为与只读缓冲区相同,只是避免了上面提到的1024字节限制,只要您传递的缓冲区至少与操作系统想要放置的缓冲区一样长,一切都应该正常。If mutate_flag is true (the default), then the buffer is (in effect) passed to the underlying如果mutate_flag为ioctl()
system call, the latter’s return code is passed back to the calling Python, and the buffer’s new contents reflect the action of theioctl()
.true
(默认值),则缓冲区(实际上)被传递给底层ioctl()
系统调用,后者的返回代码被传递回调用Python,缓冲区的新内容反映ioctl()
的操作。This is a slight simplification, because if the supplied buffer is less than 1024 bytes long it is first copied into a static buffer 1024 bytes long which is then passed to这是一个轻微的简化,因为如果提供的缓冲区长度小于1024字节,则首先将其复制到1024字节长的静态缓冲区中,然后将其传递给ioctl()
and copied back into the supplied buffer.ioctl()
并复制回提供的缓冲区时。If the如果ioctl()
fails, anOSError
exception is raised.ioctl()
失败,则会引发OSError
异常。An example:例如:>>> import array, fcntl, struct, termios, os
>>> os.getpgrp()
13341
>>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0]
13341
>>> buf = array.array('h', [0])
>>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
0
>>> buf
array('h', [13341])Raises an auditing event使用参数fcntl.ioctl
with argumentsfd
,request
,arg
.fd
、request
、arg
引发审核事件fcntl.ioctl
。
-
fcntl.
flock
(fd, operation)¶ Perform the lock operation operation on file descriptor fd (file objects providing a对文件描述符fd执行锁定操作operation(也接受提供fileno()
method are accepted as well).fileno()
方法的文件对象)。See the Unix manual flock(2) for details.有关详细信息,请参阅Unix手册flock(2)。(On some systems, this function is emulated using(在某些系统上,此函数是使用fcntl()
.)fcntl()
模拟的。)If the如果flock()
fails, anOSError
exception is raised.flock()
失败,则会引发OSError
异常。Raises an auditing event使用参数fcntl.flock
with argumentsfd
,operation
.fd
、operation
引发审核事件fcntl.flock
。
-
fcntl.
lockf
(fd, cmd, len=0, start=0, whence=0)¶ This is essentially a wrapper around the这本质上是一个围绕fcntl()
locking calls.fcntl()
锁定调用的包装器。fd is the file descriptor (file objects providing afd是要锁定或解锁的文件的文件描述符(也接受提供fileno()
method are accepted as well) of the file to lock or unlock, and cmd is one of the following values:fileno()
方法的文件对象),cmd是以下值之一:LOCK_UN
– unlockLOCK_SH
–acquire a shared lock获取共享锁LOCK_EX
–acquire an exclusive lock获取独占锁
When cmd is当cmd为LOCK_SH
orLOCK_EX
, it can also be bitwise ORed withLOCK_NB
to avoid blocking on lock acquisition.LOCK_SH
或LOCK_EX
时,它也可以与LOCK_NB
进行位“或”运算,以避免锁定获取时的阻塞。If如果使用了LOCK_NB
is used and the lock cannot be acquired, anOSError
will be raised and the exception will have an errno attribute set toEACCES
orEAGAIN
(depending on the operating system; for portability, check for both values).LOCK_N
B并且无法获取锁,则将引发一个OSError
,并且异常的errno属性将设置为EACCES
或EAGAIN
(取决于操作系统;为了便于移植,请检查这两个值)。On at least some systems,至少在某些系统上,只有当文件描述符引用为写入而打开的文件时,才能使用LOCK_EX
can only be used if the file descriptor refers to a file opened for writing.LOCK_EX
。len is the number of bytes to lock, start is the byte offset at which the lock starts, relative to whence, and whence is as withlen是要锁定的字节数,start是锁定开始的字节偏移量,相对于where,where与io.IOBase.seek()
, specifically:io.IOBase.seek()
相同,具体而言:0
–relative to the start of the file (相对于文件的开头(os.SEEK_SET
)os.SEEK_SET
)1
–relative to the current buffer position (相对于当前缓冲区位置(os.SEEK_CUR
)os.SEEK_CUR
)2
–relative to the end of the file (相对于文件末尾(os.SEEK_END
)os.SEEK_END
)
The default for start is 0, which means to start at the beginning of the file.start的默认值为0,这意味着从文件的开头开始。The default for len is 0 which means to lock to the end of the file.len的默认值是0,这意味着锁定到文件的末尾。The default for whence is also 0.where的默认值也是0。Raises an auditing event使用参数fcntl.lockf
with argumentsfd
,cmd
,len
,start
,whence
.fd
、cmd
、len
、start
和where
引发审核事件fcntllockf。
Examples (all on a SVR4 compliant system):示例(均在SVR4兼容系统上):
import struct, fcntl, os
f = open(...)
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
Note that in the first example the return value variable rv will hold an integer value; in the second example it will hold a 请注意,在第一个示例中,返回值变量rv将包含一个整数值;在第二个示例中,它将保存一个bytes
object. bytes
对象。The structure lay-out for the lockdata variable is system dependent — therefore using the lockdata变量的结构布局取决于系统,因此使用flock()
call may be better.flock()
调用可能会更好。