“Python/docs/3.9/library/fcntl”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/fcntl
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:fcntl — fcntl 和 ioctl 系统调用 — Python 文档}}
 
<div id="module-fcntl" class="section">
 
<div id="module-fcntl" class="section">
  
 
<span id="fcntl-the-fcntl-and-ioctl-system-calls"></span>
 
<span id="fcntl-the-fcntl-and-ioctl-system-calls"></span>
= [[#module-fcntl|<code>fcntl</code>]] --- The <code>fcntl</code> and <code>ioctl</code> system calls =
+
= fcntl fcntl ioctl 系统调用 =
  
This module performs file control and I/O control on file descriptors. It is an
 
interface to the <code>fcntl()</code> and <code>ioctl()</code> Unix routines. For a
 
complete description of these calls, see ''[https://manpages.debian.org/fcntl(2) fcntl(2)]'' and
 
''[https://manpages.debian.org/ioctl(2) ioctl(2)]'' Unix manual pages.
 
  
All functions in this module take a file descriptor ''fd'' as their first
+
-----
argument. This can be an integer file descriptor, such as returned by
+
 
<code>sys.stdin.fileno()</code>, or an [[../io#io|<code>io.IOBase</code>]] object, such as <code>sys.stdin</code>
+
该模块对文件描述符执行文件控制和 I/O 控制。 它是 <code>fcntl()</code> 和 <code>ioctl()</code> Unix 例程的接口。 有关这些调用的完整说明,请参阅 ''fcntl(2)'' 和 ''ioctl(2)'' Unix 手册页。
itself, which provides a [[../io#io.IOBase|<code>fileno()</code>]] that returns a genuine file
+
 
descriptor.
+
该模块中的所有函数都将文件描述符 ''fd'' 作为它们的第一个参数。 这可以是一个整数文件描述符,例如由 <code>sys.stdin.fileno()</code> 返回,或一个 [[../io#io|io.IOBase]] 对象,例如 <code>sys.stdin</code> 本身,它提供了一个 [[../io#io.IOBase|fileno( )]] 返回一个真正的文件描述符。
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">在 3.3 版更改: </span>Operations in this module used to raise an [[../exceptions#IOError|<code>IOError</code>]] where they now
+
<span class="versionmodified changed">在 3.3 版中更改:</span>此模块中的操作用于引发 [[../exceptions#IOError|IOError]],它们现在引发 [[../exceptions#OSError|OSError]]
raise an [[../exceptions#OSError|<code>OSError</code>]].
 
  
  
第24行: 第20行:
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">3.8 版更改: </span>The fcntl module now contains <code>F_ADD_SEALS</code>, <code>F_GET_SEALS</code>, and
+
<span class="versionmodified changed"> 3.8 版更改: </span> fcntl 模块现在包含 <code>F_ADD_SEALS</code><code>F_GET_SEALS</code> <code>F_SEAL_*</code> 常量,用于密封 os.memfd_create()[ X145X] 文件描述符。
<code>F_SEAL_*</code> constants for sealing of [[../os#os|<code>os.memfd_create()</code>]] file
 
descriptors.
 
  
  
第32行: 第26行:
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">3.9 版更改: </span>On macOS, the fcntl module exposes the <code>F_GETPATH</code> constant, which obtains
+
<span class="versionmodified changed"> 3.9 更改: </span> macOS 上,fcntl 模块公开了 <code>F_GETPATH</code> 常量,该常量从文件描述符中获取文件的路径。 在 Linux(&gt;=3.15) 上,fcntl 模块公开了 <code>F_OFD_GETLK</code><code>F_OFD_SETLK</code> <code>F_OFD_SETLKW</code> 常量,这些常量与打开的文件描述锁一起使用。
the path of a file from a file descriptor.
 
On Linux(&gt;=3.15), the fcntl module exposes the <code>F_OFD_GETLK</code>, <code>F_OFD_SETLK</code>
 
and <code>F_OFD_SETLKW</code> constants, which working with open file description locks.
 
  
  
 
</div>
 
</div>
The module defines the following functions:
+
该模块定义了以下功能:
  
 
<dl>
 
<dl>
<dt><code>fcntl.</code><code>fcntl</code><span class="sig-paren">(</span>''<span class="n">fd</span>'', ''<span class="n">cmd</span>'', ''<span class="n">arg</span><span class="o">=</span><span class="default_value">0</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">fcntl</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fd</span></span>'', ''<span class="n"><span class="pre">cmd</span></span>'', ''<span class="n"><span class="pre">arg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Perform the operation ''cmd'' on file descriptor ''fd'' (file objects providing
+
<dd><p>对文件描述符 ''fd'' 执行操作 ''cmd''(也接受提供 [[../io#io.IOBase|fileno()]] 方法的文件对象)。 用于 ''cmd'' 的值取决于操作系统,并且在 [[#module-fcntl|fcntl]] 模块中作为常量提供,使用与相关 C 头文件中使用的名称相同的名称。 参数 ''arg'' 可以是整数值,也可以是 [[../stdtypes#bytes|bytes]] 对象。 使用整数值,该函数的返回值是C<code>fcntl()</code>调用的整数返回值。 当参数是字节时,它表示一个二进制结构,例如 由 [[../struct#struct|struct.pack()]] 创建。 二进制数据被复制到一个缓冲区,其地址被传递给 C <code>fcntl()</code> 调用。 成功调用后的返回值是缓冲区的内容,转换为 [[../stdtypes#bytes|bytes]] 对象。 返回对象的长度将与 ''arg'' 参数的长度相同。 这被限制为 1024 字节。 如果操作系统在缓冲区中返回的信息大于 1024 字节,这很可能会导致分段违规或更细微的数据损坏。</p>
a [[../io#io.IOBase|<code>fileno()</code>]] method are accepted as well). The values used
+
<p>如果 <code>fcntl()</code> 失败,则会引发 [[../exceptions#OSError|OSError]]</p></dd></dl>
for ''cmd'' are operating system dependent, and are available as constants
 
in the [[#module-fcntl|<code>fcntl</code>]] module, using the same names as used in the relevant C
 
header files. The argument ''arg'' can either be an integer value, or a
 
[[../stdtypes#bytes|<code>bytes</code>]] object. With an integer value, the return value of this
 
function is the integer return value of the C <code>fcntl()</code> call. When
 
the argument is bytes it represents a binary structure, e.g. created by
 
[[../struct#struct|<code>struct.pack()</code>]]. The binary data is copied to a buffer whose address is
 
passed to the C <code>fcntl()</code> call. The return value after a successful
 
call is the contents of the buffer, converted to a [[../stdtypes#bytes|<code>bytes</code>]] object.
 
The length of the returned object will be the same as the length of the
 
''arg'' argument. This is limited to 1024 bytes. 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.</p>
 
<p>If the <code>fcntl()</code> fails, an [[../exceptions#OSError|<code>OSError</code>]] is raised.</p>
 
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>fcntl.fcntl</code> with arguments <code>fd</code>, <code>cmd</code>, <code>arg</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>fcntl.</code><code>ioctl</code><span class="sig-paren">(</span>''<span class="n">fd</span>'', ''<span class="n">request</span>'', ''<span class="n">arg</span><span class="o">=</span><span class="default_value">0</span>'', ''<span class="n">mutate_flag</span><span class="o">=</span><span class="default_value">True</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">ioctl</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fd</span></span>'', ''<span class="n"><span class="pre">request</span></span>'', ''<span class="n"><span class="pre">arg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span>'', ''<span class="n"><span class="pre">mutate_flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>This function is identical to the [[#fcntl.fcntl|<code>fcntl()</code>]] function, except
+
<dd><p>该函数与 [[#fcntl.fcntl|fcntl()]] 函数相同,只是参数处理更加复杂。</p>
that the argument handling is even more complicated.</p>
+
<p>''request'' 参数仅限于可以容纳 32 位的值。 可以在 [[../termios#module-termios|termios]] 模块中找到用作 ''request'' 参数的其他感兴趣的常量,其名称与相关 C 头文件中使用的名称相同。</p>
<p>The ''request'' parameter is limited to values that can fit in 32-bits.
+
<p>参数 ''arg'' 可以是整数、支持只读缓冲区接口的对象(如 [[../stdtypes#bytes|bytes]])或支持读写缓冲区接口的对象(如 [[../stdtypes#bytearray|)之一字节数组]])。</p>
Additional constants of interest for use as the ''request'' argument can be
+
<p>除了最后一种情况外,所有行为都与 [[#fcntl.fcntl|fcntl()]] 函数相同。</p>
found in the [[../termios#module-termios|<code>termios</code>]] module, under the same names as used in
+
<p>如果传递了可变缓冲区,则行为由 ''mutate_flag'' 参数的值决定。</p>
the relevant C header files.</p>
+
<p>如果为 false,则缓冲区的可变性将被忽略,行为与只读缓冲区相同,只是避免了上面提到的 1024 字节限制——只要你传递的缓冲区至少与操作系统想要的一样长放在那里,事情应该工作。</p>
<p>The parameter ''arg'' can be one of an integer, an object supporting the
+
<p>如果 ''mutate_flag'' 为 true(默认值),则缓冲区(实际上)传递给底层 [[#fcntl.ioctl|ioctl()]] 系统调用,后者的返回码传递回调用 Python ,缓冲区的新内容反映了 [[#fcntl.ioctl|ioctl()]] 的操作。 这是一个轻微的简化,因为如果提供的缓冲区长度小于 1024 字节,则首先将其复制到 1024 字节长的静态缓冲区中,然后将其传递给 [[#fcntl.ioctl|ioctl()]] 并复制回提供的缓冲区中。</p>
read-only buffer interface (like [[../stdtypes#bytes|<code>bytes</code>]]) or an object supporting
+
<p>如果 <code>ioctl()</code> 失败,则会引发 [[../exceptions#OSError|OSError]] 异常。</p>
the read-write buffer interface (like [[../stdtypes#bytearray|<code>bytearray</code>]]).</p>
+
<p>一个例子:</p>
<p>In all but the last case, behaviour is as for the [[#fcntl.fcntl|<code>fcntl()</code>]]
 
function.</p>
 
<p>If a mutable buffer is passed, then the behaviour is determined by the value of
 
the ''mutate_flag'' parameter.</p>
 
<p>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.</p>
 
<p>If ''mutate_flag'' is true (the default), then the buffer is (in effect) passed
 
to the underlying [[#fcntl.ioctl|<code>ioctl()</code>]] system call, the latter's return code is
 
passed back to the calling Python, and the buffer's new contents reflect the
 
action of the [[#fcntl.ioctl|<code>ioctl()</code>]]. 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 [[#fcntl.ioctl|<code>ioctl()</code>]] and copied back
 
into the supplied buffer.</p>
 
<p>If the <code>ioctl()</code> fails, an [[../exceptions#OSError|<code>OSError</code>]] exception is raised.</p>
 
<p>An example:</p>
 
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; import array, fcntl, struct, termios, os
+
<syntaxhighlight lang="python3">>>> import array, fcntl, struct, termios, os
&gt;&gt;&gt; os.getpgrp()
+
>>> os.getpgrp()
 
13341
 
13341
&gt;&gt;&gt; struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, &quot; &quot;))[0]
+
>>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, " "))[0]
 
13341
 
13341
&gt;&gt;&gt; buf = array.array('h', [0])
+
>>> buf = array.array('h', [0])
&gt;&gt;&gt; fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
+
>>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
 
0
 
0
&gt;&gt;&gt; buf
+
>>> buf
array('h', [13341])</pre>
+
array('h', [13341])</syntaxhighlight>
  
 
</div>
 
</div>
  
</div>
+
</div></dd></dl>
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>fcntl.ioctl</code> with arguments <code>fd</code>, <code>request</code>, <code>arg</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>fcntl.</code><code>flock</code><span class="sig-paren">(</span>''<span class="n">fd</span>'', ''<span class="n">operation</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">flock</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fd</span></span>'', ''<span class="n"><span class="pre">operation</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Perform the lock operation ''operation'' on file descriptor ''fd'' (file objects providing
+
<dd><p>对文件描述符 ''fd'' 执行锁定操作 ''operation''(也接受提供 [[../io#io.IOBase|fileno()]] 方法的文件对象)。 有关详细信息,请参阅 Unix 手册 ''flock(2)''。 (在某些系统上,使用 <code>fcntl()</code> 模拟此功能。)</p>
a [[../io#io.IOBase|<code>fileno()</code>]] method are accepted as well). See the Unix manual
+
<p>如果 <code>flock()</code> 失败,则会引发 [[../exceptions#OSError|OSError]] 异常。</p></dd></dl>
''[https://manpages.debian.org/flock(2) flock(2)]'' for details. (On some systems, this function is emulated
 
using <code>fcntl()</code>.)</p>
 
<p>If the <code>flock()</code> fails, an [[../exceptions#OSError|<code>OSError</code>]] exception is raised.</p>
 
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>fcntl.flock</code> with arguments <code>fd</code>, <code>operation</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>fcntl.</code><code>lockf</code><span class="sig-paren">(</span>''<span class="n">fd</span>'', ''<span class="n">cmd</span>'', ''<span class="n">len</span><span class="o">=</span><span class="default_value">0</span>'', ''<span class="n">start</span><span class="o">=</span><span class="default_value">0</span>'', ''<span class="n">whence</span><span class="o">=</span><span class="default_value">0</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">fcntl.</span></span><span class="sig-name descname"><span class="pre">lockf</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">fd</span></span>'', ''<span class="n"><span class="pre">cmd</span></span>'', ''<span class="n"><span class="pre">len</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span>'', ''<span class="n"><span class="pre">start</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span>'', ''<span class="n"><span class="pre">whence</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>This is essentially a wrapper around the [[#fcntl.fcntl|<code>fcntl()</code>]] locking calls.
+
<dd><p>这本质上是对 [[#fcntl.fcntl|fcntl()]] 锁定调用的封装。 ''fd'' 是要锁定或解锁的文件的文件描述符(也接受提供 [[../io#io.IOBase|fileno()]] 方法的文件对象),''cmd'' 是其中之一以下值:</p>
''fd'' is the file descriptor (file objects providing a [[../io#io.IOBase|<code>fileno()</code>]]
 
method are accepted as well) of the file to lock or unlock, and ''cmd''
 
is one of the following values:</p>
 
 
<ul>
 
<ul>
<li><p><code>LOCK_UN</code> -- unlock</p></li>
+
<li><p><code>LOCK_UN</code> – 解锁</p></li>
<li><p><code>LOCK_SH</code> -- acquire a shared lock</p></li>
+
<li><p><code>LOCK_SH</code> – 获取共享锁</p></li>
<li><p><code>LOCK_EX</code> -- acquire an exclusive lock</p></li></ul>
+
<li><p><code>LOCK_EX</code> – 获取排他锁</p></li></ul>
  
<p>When ''cmd'' is <code>LOCK_SH</code> or <code>LOCK_EX</code>, it can also be
+
<p>''cmd'' <code>LOCK_SH</code> <code>LOCK_EX</code> 时,也可以与 <code>LOCK_NB</code> 进行按位或运算,以避免锁获取阻塞。 如果使用 <code>LOCK_NB</code> 并且无法获取锁,则会引发 [[../exceptions#OSError|OSError]] 并且异常会将 ''errno'' 属性设置为 <code>EACCES</code> <code>EAGAIN</code>(取决于操作系统;为了可移植性,检查两个值)。 至少在某些系统上,<code>LOCK_EX</code> 只能在文件描述符引用为写入而打开的文件时使用。</p>
bitwise ORed with <code>LOCK_NB</code> to avoid blocking on lock acquisition.
+
<p>''len'' 是要锁定的字节数,''start'' 是锁定开始的字节偏移量,相对于 ''whence'' ''whence''[[../io#io.IOBase|io.IOBase.seek()]] 一样,特别是:</p>
If <code>LOCK_NB</code> is used and the lock cannot be acquired, an
 
[[../exceptions#OSError|<code>OSError</code>]] will be raised and the exception will have an ''errno''
 
attribute set to <code>EACCES</code> or <code>EAGAIN</code> (depending on the
 
operating system; for portability, check for both values). On at least some
 
systems, <code>LOCK_EX</code> can only be used if the file descriptor refers to a
 
file opened for writing.</p>
 
<p>''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 with
 
[[../io#io.IOBase|<code>io.IOBase.seek()</code>]], specifically:</p>
 
 
<ul>
 
<ul>
<li><p><code>0</code> -- relative to the start of the file ([[../os#os|<code>os.SEEK_SET</code>]])</p></li>
+
<li><p><code>0</code> – 相对于文件的开头 ([[../os#os|os.SEEK_SET]])</p></li>
<li><p><code>1</code> -- relative to the current buffer position ([[../os#os|<code>os.SEEK_CUR</code>]])</p></li>
+
<li><p><code>1</code> – 相对于当前缓冲区位置 ([[../os#os|os.SEEK_CUR]])</p></li>
<li><p><code>2</code> -- relative to the end of the file ([[../os#os|<code>os.SEEK_END</code>]])</p></li></ul>
+
<li><p><code>2</code> – 相对于文件结尾 ([[../os#os|os.SEEK_END]])</p></li></ul>
  
<p>The default for ''start'' is 0, which means to start at the beginning of the file.
+
<p>''start'' 的默认值为 0,表示从文件的开头开始。 ''len'' 的默认值为 0,表示锁定到文件末尾。 ''whence'' 的默认值也是 0。</p></dd></dl>
The default for ''len'' is 0 which means to lock to the end of the file. The
 
default for ''whence'' is also 0.</p>
 
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>fcntl.lockf</code> with arguments <code>fd</code>, <code>cmd</code>, <code>len</code>, <code>start</code>, <code>whence</code>.</p></dd></dl>
 
  
Examples (all on a SVR4 compliant system):
+
示例(均在 SVR4 兼容系统上):
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第157行: 第95行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import struct, fcntl, os
+
<syntaxhighlight lang="python3">import struct, fcntl, os
  
 
f = open(...)
 
f = open(...)
第163行: 第101行:
  
 
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
 
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)</pre>
+
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Note that in the first example the return value variable ''rv'' will hold an
+
请注意,在第一个示例中,返回值变量 ''rv'' 将保存一个整数值; 在第二个示例中,它将保存一个 [[../stdtypes#bytes|bytes]] 对象。 ''lockdata'' 变量的结构布局取决于系统——因此使用 [[#fcntl.flock|flock()]] 调用可能更好。
integer value; in the second example it will hold a [[../stdtypes#bytes|<code>bytes</code>]] object. The
 
structure lay-out for the ''lockdata'' variable is system dependent --- therefore
 
using the [[#fcntl.flock|<code>flock()</code>]] call may be better.
 
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; Module [[../os#module-os|<code>os</code>]]
+
; 模块 [[../os#module-os|os]]
: If the locking flags [[../os#os|<code>O_SHLOCK</code>]] and [[../os#os|<code>O_EXLOCK</code>]] are present in the [[../os#module-os|<code>os</code>]] module (on BSD only), the [[../os#os|<code>os.open()</code>]] function provides an alternative to the [[#fcntl.lockf|<code>lockf()</code>]] and [[#fcntl.flock|<code>flock()</code>]] functions.
+
: 如果锁定标志 [[../os#os|O_SHLOCK]] [[../os#os|O_EXLOCK]] 存在于 [[../os#module-os|os]] 模块中(仅在 BSD 上),则 [[../os#os|os.open()]] 函数提供了 [[#fcntl.lockf|lockf()]] [[#fcntl.flock|flock()]] 函数的替代方法。
  
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:52的最新版本

fcntl — fcntl 和 ioctl 系统调用


该模块对文件描述符执行文件控制和 I/O 控制。 它是 fcntl()ioctl() Unix 例程的接口。 有关这些调用的完整说明,请参阅 fcntl(2)ioctl(2) Unix 手册页。

该模块中的所有函数都将文件描述符 fd 作为它们的第一个参数。 这可以是一个整数文件描述符,例如由 sys.stdin.fileno() 返回,或一个 io.IOBase 对象,例如 sys.stdin 本身,它提供了一个 fileno( ) 返回一个真正的文件描述符。

在 3.3 版中更改:此模块中的操作用于引发 IOError,它们现在引发 OSError


3.8 版更改: fcntl 模块现在包含 F_ADD_SEALSF_GET_SEALSF_SEAL_* 常量,用于密封 os.memfd_create()[ X145X] 文件描述符。


3.9 更改: 在 macOS 上,fcntl 模块公开了 F_GETPATH 常量,该常量从文件描述符中获取文件的路径。 在 Linux(>=3.15) 上,fcntl 模块公开了 F_OFD_GETLKF_OFD_SETLKF_OFD_SETLKW 常量,这些常量与打开的文件描述锁一起使用。


该模块定义了以下功能:

fcntl.fcntl(fd, cmd, arg=0)

对文件描述符 fd 执行操作 cmd(也接受提供 fileno() 方法的文件对象)。 用于 cmd 的值取决于操作系统,并且在 fcntl 模块中作为常量提供,使用与相关 C 头文件中使用的名称相同的名称。 参数 arg 可以是整数值,也可以是 bytes 对象。 使用整数值,该函数的返回值是Cfcntl()调用的整数返回值。 当参数是字节时,它表示一个二进制结构,例如 由 struct.pack() 创建。 二进制数据被复制到一个缓冲区,其地址被传递给 C fcntl() 调用。 成功调用后的返回值是缓冲区的内容,转换为 bytes 对象。 返回对象的长度将与 arg 参数的长度相同。 这被限制为 1024 字节。 如果操作系统在缓冲区中返回的信息大于 1024 字节,这很可能会导致分段违规或更细微的数据损坏。

如果 fcntl() 失败,则会引发 OSError

fcntl.ioctl(fd, request, arg=0, mutate_flag=True)

该函数与 fcntl() 函数相同,只是参数处理更加复杂。

request 参数仅限于可以容纳 32 位的值。 可以在 termios 模块中找到用作 request 参数的其他感兴趣的常量,其名称与相关 C 头文件中使用的名称相同。

参数 arg 可以是整数、支持只读缓冲区接口的对象(如 bytes)或支持读写缓冲区接口的对象(如 )之一字节数组)。

除了最后一种情况外,所有行为都与 fcntl() 函数相同。

如果传递了可变缓冲区,则行为由 mutate_flag 参数的值决定。

如果为 false,则缓冲区的可变性将被忽略,行为与只读缓冲区相同,只是避免了上面提到的 1024 字节限制——只要你传递的缓冲区至少与操作系统想要的一样长放在那里,事情应该工作。

如果 mutate_flag 为 true(默认值),则缓冲区(实际上)传递给底层 ioctl() 系统调用,后者的返回码传递回调用 Python ,缓冲区的新内容反映了 ioctl() 的操作。 这是一个轻微的简化,因为如果提供的缓冲区长度小于 1024 字节,则首先将其复制到 1024 字节长的静态缓冲区中,然后将其传递给 ioctl() 并复制回提供的缓冲区中。

如果 ioctl() 失败,则会引发 OSError 异常。

一个例子:

>>> 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])
fcntl.flock(fd, operation)

对文件描述符 fd 执行锁定操作 operation(也接受提供 fileno() 方法的文件对象)。 有关详细信息,请参阅 Unix 手册 flock(2)。 (在某些系统上,使用 fcntl() 模拟此功能。)

如果 flock() 失败,则会引发 OSError 异常。

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

这本质上是对 fcntl() 锁定调用的封装。 fd 是要锁定或解锁的文件的文件描述符(也接受提供 fileno() 方法的文件对象),cmd 是其中之一以下值:

  • LOCK_UN – 解锁

  • LOCK_SH – 获取共享锁

  • LOCK_EX – 获取排他锁

cmdLOCK_SHLOCK_EX 时,也可以与 LOCK_NB 进行按位或运算,以避免锁获取阻塞。 如果使用 LOCK_NB 并且无法获取锁,则会引发 OSError 并且异常会将 errno 属性设置为 EACCESEAGAIN(取决于操作系统;为了可移植性,检查两个值)。 至少在某些系统上,LOCK_EX 只能在文件描述符引用为写入而打开的文件时使用。

len 是要锁定的字节数,start 是锁定开始的字节偏移量,相对于 whencewhenceio.IOBase.seek() 一样,特别是:

start 的默认值为 0,表示从文件的开头开始。 len 的默认值为 0,表示锁定到文件末尾。 whence 的默认值也是 0。

示例(均在 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)

请注意,在第一个示例中,返回值变量 rv 将保存一个整数值; 在第二个示例中,它将保存一个 bytes 对象。 lockdata 变量的结构布局取决于系统——因此使用 flock() 调用可能更好。

也可以看看

模块 os
如果锁定标志 O_SHLOCKO_EXLOCK 存在于 os 模块中(仅在 BSD 上),则 os.open() 函数提供了 lockf()flock() 函数的替代方法。