gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

654 lines
45KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="selectors — High-level I/O multiplexing" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/selectors.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/selectors.py Introduction: This module allows high-level and efficient I/O multiplexing, built upon the select module primitives. Users are encouraged to use this module instead, u..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/selectors.py Introduction: This module allows high-level and efficient I/O multiplexing, built upon the select module primitives. Users are encouraged to use this module instead, u..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>selectors — High-level I/O multiplexing &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="signal — Set handlers for asynchronous events" href="signal.html" />
  33. <link rel="prev" title="select — Waiting for I/O completion" href="select.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/selectors.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code> — High-level I/O multiplexing</a><ul>
  86. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  87. <li><a class="reference internal" href="#classes">Classes</a></li>
  88. <li><a class="reference internal" href="#examples">Examples</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="select.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code> — Waiting for I/O completion</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="signal.html"
  101. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code> — Set handlers for asynchronous events</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/selectors.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="signal.html" title="signal — Set handlers for asynchronous events"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="select.html" title="select — Waiting for I/O completion"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">Networking and Interprocess Communication</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code> — High-level I/O multiplexing</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="module-selectors">
  171. <span id="selectors-high-level-i-o-multiplexing"></span><h1><a class="reference internal" href="#module-selectors" title="selectors: High-level I/O multiplexing."><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code></a> — High-level I/O multiplexing<a class="headerlink" href="#module-selectors" title="Link to this heading">¶</a></h1>
  172. <div class="versionadded">
  173. <p><span class="versionmodified added">New in version 3.4.</span></p>
  174. </div>
  175. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/selectors.py">Lib/selectors.py</a></p>
  176. <hr class="docutils" />
  177. <section id="introduction">
  178. <h2>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h2>
  179. <p>This module allows high-level and efficient I/O multiplexing, built upon the
  180. <a class="reference internal" href="select.html#module-select" title="select: Wait for I/O completion on multiple streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a> module primitives. Users are encouraged to use this module
  181. instead, unless they want precise control over the OS-level primitives used.</p>
  182. <p>It defines a <a class="reference internal" href="#selectors.BaseSelector" title="selectors.BaseSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseSelector</span></code></a> abstract base class, along with several
  183. concrete implementations (<a class="reference internal" href="#selectors.KqueueSelector" title="selectors.KqueueSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">KqueueSelector</span></code></a>, <a class="reference internal" href="#selectors.EpollSelector" title="selectors.EpollSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">EpollSelector</span></code></a>…),
  184. that can be used to wait for I/O readiness notification on multiple file
  185. objects. In the following, “file object” refers to any object with a
  186. <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> method, or a raw file descriptor. See <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>.</p>
  187. <p><a class="reference internal" href="#selectors.DefaultSelector" title="selectors.DefaultSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">DefaultSelector</span></code></a> is an alias to the most efficient implementation
  188. available on the current platform: this should be the default choice for most
  189. users.</p>
  190. <div class="admonition note">
  191. <p class="admonition-title">Note</p>
  192. <p>The type of file objects supported depends on the platform: on Windows,
  193. sockets are supported, but not pipes, whereas on Unix, both are supported
  194. (some other types may be supported as well, such as fifos or special file
  195. devices).</p>
  196. </div>
  197. <div class="admonition seealso">
  198. <p class="admonition-title">See also</p>
  199. <dl class="simple">
  200. <dt><a class="reference internal" href="select.html#module-select" title="select: Wait for I/O completion on multiple streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a></dt><dd><p>Low-level I/O multiplexing module.</p>
  201. </dd>
  202. </dl>
  203. </div>
  204. <div class="availability docutils container">
  205. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  206. <p>This module does not work or is not available on WebAssembly platforms
  207. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  208. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  209. </div>
  210. </section>
  211. <section id="classes">
  212. <h2>Classes<a class="headerlink" href="#classes" title="Link to this heading">¶</a></h2>
  213. <p>Classes hierarchy:</p>
  214. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">BaseSelector</span>
  215. <span class="o">+--</span> <span class="n">SelectSelector</span>
  216. <span class="o">+--</span> <span class="n">PollSelector</span>
  217. <span class="o">+--</span> <span class="n">EpollSelector</span>
  218. <span class="o">+--</span> <span class="n">DevpollSelector</span>
  219. <span class="o">+--</span> <span class="n">KqueueSelector</span>
  220. </pre></div>
  221. </div>
  222. <p>In the following, <em>events</em> is a bitwise mask indicating which I/O events should
  223. be waited for on a given file object. It can be a combination of the modules
  224. constants below:</p>
  225. <blockquote>
  226. <div><table class="docutils align-default">
  227. <thead>
  228. <tr class="row-odd"><th class="head"><p>Constant</p></th>
  229. <th class="head"><p>Meaning</p></th>
  230. </tr>
  231. </thead>
  232. <tbody>
  233. <tr class="row-even"><td><dl class="py data">
  234. <dt class="sig sig-object py" id="selectors.EVENT_READ">
  235. <span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">EVENT_READ</span></span><a class="headerlink" href="#selectors.EVENT_READ" title="Link to this definition">¶</a></dt>
  236. <dd></dd></dl>
  237. </td>
  238. <td><p>Available for read</p></td>
  239. </tr>
  240. <tr class="row-odd"><td><dl class="py data">
  241. <dt class="sig sig-object py" id="selectors.EVENT_WRITE">
  242. <span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">EVENT_WRITE</span></span><a class="headerlink" href="#selectors.EVENT_WRITE" title="Link to this definition">¶</a></dt>
  243. <dd></dd></dl>
  244. </td>
  245. <td><p>Available for write</p></td>
  246. </tr>
  247. </tbody>
  248. </table>
  249. </div></blockquote>
  250. <dl class="py class">
  251. <dt class="sig sig-object py" id="selectors.SelectorKey">
  252. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">SelectorKey</span></span><a class="headerlink" href="#selectors.SelectorKey" title="Link to this definition">¶</a></dt>
  253. <dd><p>A <a class="reference internal" href="#selectors.SelectorKey" title="selectors.SelectorKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorKey</span></code></a> is a <a class="reference internal" href="collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">namedtuple</span></code></a> used to
  254. associate a file object to its underlying file descriptor, selected event
  255. mask and attached data. It is returned by several <a class="reference internal" href="#selectors.BaseSelector" title="selectors.BaseSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseSelector</span></code></a>
  256. methods.</p>
  257. <dl class="py attribute">
  258. <dt class="sig sig-object py" id="selectors.SelectorKey.fileobj">
  259. <span class="sig-name descname"><span class="pre">fileobj</span></span><a class="headerlink" href="#selectors.SelectorKey.fileobj" title="Link to this definition">¶</a></dt>
  260. <dd><p>File object registered.</p>
  261. </dd></dl>
  262. <dl class="py attribute">
  263. <dt class="sig sig-object py" id="selectors.SelectorKey.fd">
  264. <span class="sig-name descname"><span class="pre">fd</span></span><a class="headerlink" href="#selectors.SelectorKey.fd" title="Link to this definition">¶</a></dt>
  265. <dd><p>Underlying file descriptor.</p>
  266. </dd></dl>
  267. <dl class="py attribute">
  268. <dt class="sig sig-object py" id="selectors.SelectorKey.events">
  269. <span class="sig-name descname"><span class="pre">events</span></span><a class="headerlink" href="#selectors.SelectorKey.events" title="Link to this definition">¶</a></dt>
  270. <dd><p>Events that must be waited for on this file object.</p>
  271. </dd></dl>
  272. <dl class="py attribute">
  273. <dt class="sig sig-object py" id="selectors.SelectorKey.data">
  274. <span class="sig-name descname"><span class="pre">data</span></span><a class="headerlink" href="#selectors.SelectorKey.data" title="Link to this definition">¶</a></dt>
  275. <dd><p>Optional opaque data associated to this file object: for example, this
  276. could be used to store a per-client session ID.</p>
  277. </dd></dl>
  278. </dd></dl>
  279. <dl class="py class">
  280. <dt class="sig sig-object py" id="selectors.BaseSelector">
  281. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">BaseSelector</span></span><a class="headerlink" href="#selectors.BaseSelector" title="Link to this definition">¶</a></dt>
  282. <dd><p>A <a class="reference internal" href="#selectors.BaseSelector" title="selectors.BaseSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseSelector</span></code></a> is used to wait for I/O event readiness on multiple
  283. file objects. It supports file stream registration, unregistration, and a
  284. method to wait for I/O events on those streams, with an optional timeout.
  285. It’s an abstract base class, so cannot be instantiated. Use
  286. <a class="reference internal" href="#selectors.DefaultSelector" title="selectors.DefaultSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">DefaultSelector</span></code></a> instead, or one of <a class="reference internal" href="#selectors.SelectSelector" title="selectors.SelectSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectSelector</span></code></a>,
  287. <a class="reference internal" href="#selectors.KqueueSelector" title="selectors.KqueueSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">KqueueSelector</span></code></a> etc. if you want to specifically use an
  288. implementation, and your platform supports it.
  289. <a class="reference internal" href="#selectors.BaseSelector" title="selectors.BaseSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseSelector</span></code></a> and its concrete implementations support the
  290. <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol.</p>
  291. <dl class="py method">
  292. <dt class="sig sig-object py" id="selectors.BaseSelector.register">
  293. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">register</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fileobj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">events</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#selectors.BaseSelector.register" title="Link to this definition">¶</a></dt>
  294. <dd><p>Register a file object for selection, monitoring it for I/O events.</p>
  295. <p><em>fileobj</em> is the file object to monitor. It may either be an integer
  296. file descriptor or an object with a <code class="docutils literal notranslate"><span class="pre">fileno()</span></code> method.
  297. <em>events</em> is a bitwise mask of events to monitor.
  298. <em>data</em> is an opaque object.</p>
  299. <p>This returns a new <a class="reference internal" href="#selectors.SelectorKey" title="selectors.SelectorKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorKey</span></code></a> instance, or raises a
  300. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> in case of invalid event mask or file descriptor, or
  301. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> if the file object is already registered.</p>
  302. </dd></dl>
  303. <dl class="py method">
  304. <dt class="sig sig-object py" id="selectors.BaseSelector.unregister">
  305. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">unregister</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fileobj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#selectors.BaseSelector.unregister" title="Link to this definition">¶</a></dt>
  306. <dd><p>Unregister a file object from selection, removing it from monitoring. A
  307. file object shall be unregistered prior to being closed.</p>
  308. <p><em>fileobj</em> must be a file object previously registered.</p>
  309. <p>This returns the associated <a class="reference internal" href="#selectors.SelectorKey" title="selectors.SelectorKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorKey</span></code></a> instance, or raises a
  310. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> if <em>fileobj</em> is not registered. It will raise
  311. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if <em>fileobj</em> is invalid (e.g. it has no <code class="docutils literal notranslate"><span class="pre">fileno()</span></code>
  312. method or its <code class="docutils literal notranslate"><span class="pre">fileno()</span></code> method has an invalid return value).</p>
  313. </dd></dl>
  314. <dl class="py method">
  315. <dt class="sig sig-object py" id="selectors.BaseSelector.modify">
  316. <span class="sig-name descname"><span class="pre">modify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fileobj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">events</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#selectors.BaseSelector.modify" title="Link to this definition">¶</a></dt>
  317. <dd><p>Change a registered file object’s monitored events or attached data.</p>
  318. <p>This is equivalent to <code class="docutils literal notranslate"><span class="pre">BaseSelector.unregister(fileobj)</span></code> followed
  319. by <code class="docutils literal notranslate"><span class="pre">BaseSelector.register(fileobj,</span> <span class="pre">events,</span> <span class="pre">data)</span></code>, except that it
  320. can be implemented more efficiently.</p>
  321. <p>This returns a new <a class="reference internal" href="#selectors.SelectorKey" title="selectors.SelectorKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorKey</span></code></a> instance, or raises a
  322. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> in case of invalid event mask or file descriptor, or
  323. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> if the file object is not registered.</p>
  324. </dd></dl>
  325. <dl class="py method">
  326. <dt class="sig sig-object py" id="selectors.BaseSelector.select">
  327. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">select</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#selectors.BaseSelector.select" title="Link to this definition">¶</a></dt>
  328. <dd><p>Wait until some registered file objects become ready, or the timeout
  329. expires.</p>
  330. <p>If <code class="docutils literal notranslate"><span class="pre">timeout</span> <span class="pre">&gt;</span> <span class="pre">0</span></code>, this specifies the maximum wait time, in seconds.
  331. If <code class="docutils literal notranslate"><span class="pre">timeout</span> <span class="pre">&lt;=</span> <span class="pre">0</span></code>, the call won’t block, and will report the currently
  332. ready file objects.
  333. If <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the call will block until a monitored file object
  334. becomes ready.</p>
  335. <p>This returns a list of <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">events)</span></code> tuples, one for each ready file
  336. object.</p>
  337. <p><em>key</em> is the <a class="reference internal" href="#selectors.SelectorKey" title="selectors.SelectorKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorKey</span></code></a> instance corresponding to a ready file
  338. object.
  339. <em>events</em> is a bitmask of events ready on this file object.</p>
  340. <div class="admonition note">
  341. <p class="admonition-title">Note</p>
  342. <p>This method can return before any file object becomes ready or the
  343. timeout has elapsed if the current process receives a signal: in this
  344. case, an empty list will be returned.</p>
  345. </div>
  346. <div class="versionchanged">
  347. <p><span class="versionmodified changed">Changed in version 3.5: </span>The selector is now retried with a recomputed timeout when interrupted
  348. by a signal if the signal handler did not raise an exception (see
  349. <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale), instead of returning an empty list
  350. of events before the timeout.</p>
  351. </div>
  352. </dd></dl>
  353. <dl class="py method">
  354. <dt class="sig sig-object py" id="selectors.BaseSelector.close">
  355. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#selectors.BaseSelector.close" title="Link to this definition">¶</a></dt>
  356. <dd><p>Close the selector.</p>
  357. <p>This must be called to make sure that any underlying resource is freed.
  358. The selector shall not be used once it has been closed.</p>
  359. </dd></dl>
  360. <dl class="py method">
  361. <dt class="sig sig-object py" id="selectors.BaseSelector.get_key">
  362. <span class="sig-name descname"><span class="pre">get_key</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fileobj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#selectors.BaseSelector.get_key" title="Link to this definition">¶</a></dt>
  363. <dd><p>Return the key associated with a registered file object.</p>
  364. <p>This returns the <a class="reference internal" href="#selectors.SelectorKey" title="selectors.SelectorKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorKey</span></code></a> instance associated to this file
  365. object, or raises <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> if the file object is not registered.</p>
  366. </dd></dl>
  367. <dl class="py method">
  368. <dt class="sig sig-object py" id="selectors.BaseSelector.get_map">
  369. <em class="property"><span class="pre">abstractmethod</span> </em><span class="sig-name descname"><span class="pre">get_map</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#selectors.BaseSelector.get_map" title="Link to this definition">¶</a></dt>
  370. <dd><p>Return a mapping of file objects to selector keys.</p>
  371. <p>This returns a <a class="reference internal" href="collections.abc.html#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code></a> instance mapping
  372. registered file objects to their associated <a class="reference internal" href="#selectors.SelectorKey" title="selectors.SelectorKey"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelectorKey</span></code></a>
  373. instance.</p>
  374. </dd></dl>
  375. </dd></dl>
  376. <dl class="py class">
  377. <dt class="sig sig-object py" id="selectors.DefaultSelector">
  378. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">DefaultSelector</span></span><a class="headerlink" href="#selectors.DefaultSelector" title="Link to this definition">¶</a></dt>
  379. <dd><p>The default selector class, using the most efficient implementation
  380. available on the current platform. This should be the default choice for
  381. most users.</p>
  382. </dd></dl>
  383. <dl class="py class">
  384. <dt class="sig sig-object py" id="selectors.SelectSelector">
  385. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">SelectSelector</span></span><a class="headerlink" href="#selectors.SelectSelector" title="Link to this definition">¶</a></dt>
  386. <dd><p><a class="reference internal" href="select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.select()</span></code></a>-based selector.</p>
  387. </dd></dl>
  388. <dl class="py class">
  389. <dt class="sig sig-object py" id="selectors.PollSelector">
  390. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">PollSelector</span></span><a class="headerlink" href="#selectors.PollSelector" title="Link to this definition">¶</a></dt>
  391. <dd><p><a class="reference internal" href="select.html#select.poll" title="select.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.poll()</span></code></a>-based selector.</p>
  392. </dd></dl>
  393. <dl class="py class">
  394. <dt class="sig sig-object py" id="selectors.EpollSelector">
  395. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">EpollSelector</span></span><a class="headerlink" href="#selectors.EpollSelector" title="Link to this definition">¶</a></dt>
  396. <dd><p><a class="reference internal" href="select.html#select.epoll" title="select.epoll"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.epoll()</span></code></a>-based selector.</p>
  397. <dl class="py method">
  398. <dt class="sig sig-object py" id="selectors.EpollSelector.fileno">
  399. <span class="sig-name descname"><span class="pre">fileno</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#selectors.EpollSelector.fileno" title="Link to this definition">¶</a></dt>
  400. <dd><p>This returns the file descriptor used by the underlying
  401. <a class="reference internal" href="select.html#select.epoll" title="select.epoll"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.epoll()</span></code></a> object.</p>
  402. </dd></dl>
  403. </dd></dl>
  404. <dl class="py class">
  405. <dt class="sig sig-object py" id="selectors.DevpollSelector">
  406. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">DevpollSelector</span></span><a class="headerlink" href="#selectors.DevpollSelector" title="Link to this definition">¶</a></dt>
  407. <dd><p><a class="reference internal" href="select.html#select.devpoll" title="select.devpoll"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.devpoll()</span></code></a>-based selector.</p>
  408. <dl class="py method">
  409. <dt class="sig sig-object py" id="selectors.DevpollSelector.fileno">
  410. <span class="sig-name descname"><span class="pre">fileno</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#selectors.DevpollSelector.fileno" title="Link to this definition">¶</a></dt>
  411. <dd><p>This returns the file descriptor used by the underlying
  412. <a class="reference internal" href="select.html#select.devpoll" title="select.devpoll"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.devpoll()</span></code></a> object.</p>
  413. </dd></dl>
  414. <div class="versionadded">
  415. <p><span class="versionmodified added">New in version 3.5.</span></p>
  416. </div>
  417. </dd></dl>
  418. <dl class="py class">
  419. <dt class="sig sig-object py" id="selectors.KqueueSelector">
  420. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">selectors.</span></span><span class="sig-name descname"><span class="pre">KqueueSelector</span></span><a class="headerlink" href="#selectors.KqueueSelector" title="Link to this definition">¶</a></dt>
  421. <dd><p><a class="reference internal" href="select.html#select.kqueue" title="select.kqueue"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.kqueue()</span></code></a>-based selector.</p>
  422. <dl class="py method">
  423. <dt class="sig sig-object py" id="selectors.KqueueSelector.fileno">
  424. <span class="sig-name descname"><span class="pre">fileno</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#selectors.KqueueSelector.fileno" title="Link to this definition">¶</a></dt>
  425. <dd><p>This returns the file descriptor used by the underlying
  426. <a class="reference internal" href="select.html#select.kqueue" title="select.kqueue"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.kqueue()</span></code></a> object.</p>
  427. </dd></dl>
  428. </dd></dl>
  429. </section>
  430. <section id="examples">
  431. <h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  432. <p>Here is a simple echo server implementation:</p>
  433. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">selectors</span>
  434. <span class="kn">import</span> <span class="nn">socket</span>
  435. <span class="n">sel</span> <span class="o">=</span> <span class="n">selectors</span><span class="o">.</span><span class="n">DefaultSelector</span><span class="p">()</span>
  436. <span class="k">def</span> <span class="nf">accept</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">mask</span><span class="p">):</span>
  437. <span class="n">conn</span><span class="p">,</span> <span class="n">addr</span> <span class="o">=</span> <span class="n">sock</span><span class="o">.</span><span class="n">accept</span><span class="p">()</span> <span class="c1"># Should be ready</span>
  438. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;accepted&#39;</span><span class="p">,</span> <span class="n">conn</span><span class="p">,</span> <span class="s1">&#39;from&#39;</span><span class="p">,</span> <span class="n">addr</span><span class="p">)</span>
  439. <span class="n">conn</span><span class="o">.</span><span class="n">setblocking</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
  440. <span class="n">sel</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">selectors</span><span class="o">.</span><span class="n">EVENT_READ</span><span class="p">,</span> <span class="n">read</span><span class="p">)</span>
  441. <span class="k">def</span> <span class="nf">read</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">mask</span><span class="p">):</span>
  442. <span class="n">data</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">recv</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="c1"># Should be ready</span>
  443. <span class="k">if</span> <span class="n">data</span><span class="p">:</span>
  444. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;echoing&#39;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="s1">&#39;to&#39;</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span>
  445. <span class="n">conn</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="c1"># Hope it won&#39;t block</span>
  446. <span class="k">else</span><span class="p">:</span>
  447. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;closing&#39;</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span>
  448. <span class="n">sel</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span>
  449. <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  450. <span class="n">sock</span> <span class="o">=</span> <span class="n">socket</span><span class="o">.</span><span class="n">socket</span><span class="p">()</span>
  451. <span class="n">sock</span><span class="o">.</span><span class="n">bind</span><span class="p">((</span><span class="s1">&#39;localhost&#39;</span><span class="p">,</span> <span class="mi">1234</span><span class="p">))</span>
  452. <span class="n">sock</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
  453. <span class="n">sock</span><span class="o">.</span><span class="n">setblocking</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
  454. <span class="n">sel</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">selectors</span><span class="o">.</span><span class="n">EVENT_READ</span><span class="p">,</span> <span class="n">accept</span><span class="p">)</span>
  455. <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  456. <span class="n">events</span> <span class="o">=</span> <span class="n">sel</span><span class="o">.</span><span class="n">select</span><span class="p">()</span>
  457. <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">mask</span> <span class="ow">in</span> <span class="n">events</span><span class="p">:</span>
  458. <span class="n">callback</span> <span class="o">=</span> <span class="n">key</span><span class="o">.</span><span class="n">data</span>
  459. <span class="n">callback</span><span class="p">(</span><span class="n">key</span><span class="o">.</span><span class="n">fileobj</span><span class="p">,</span> <span class="n">mask</span><span class="p">)</span>
  460. </pre></div>
  461. </div>
  462. </section>
  463. </section>
  464. <div class="clearer"></div>
  465. </div>
  466. </div>
  467. </div>
  468. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  469. <div class="sphinxsidebarwrapper">
  470. <div>
  471. <h3><a href="../contents.html">Table of Contents</a></h3>
  472. <ul>
  473. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code> — High-level I/O multiplexing</a><ul>
  474. <li><a class="reference internal" href="#introduction">Introduction</a></li>
  475. <li><a class="reference internal" href="#classes">Classes</a></li>
  476. <li><a class="reference internal" href="#examples">Examples</a></li>
  477. </ul>
  478. </li>
  479. </ul>
  480. </div>
  481. <div>
  482. <h4>Previous topic</h4>
  483. <p class="topless"><a href="select.html"
  484. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code> — Waiting for I/O completion</a></p>
  485. </div>
  486. <div>
  487. <h4>Next topic</h4>
  488. <p class="topless"><a href="signal.html"
  489. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code> — Set handlers for asynchronous events</a></p>
  490. </div>
  491. <div role="note" aria-label="source link">
  492. <h3>This Page</h3>
  493. <ul class="this-page-menu">
  494. <li><a href="../bugs.html">Report a Bug</a></li>
  495. <li>
  496. <a href="https://github.com/python/cpython/blob/main/Doc/library/selectors.rst"
  497. rel="nofollow">Show Source
  498. </a>
  499. </li>
  500. </ul>
  501. </div>
  502. </div>
  503. <div id="sidebarbutton" title="Collapse sidebar">
  504. <span>«</span>
  505. </div>
  506. </div>
  507. <div class="clearer"></div>
  508. </div>
  509. <div class="related" role="navigation" aria-label="related navigation">
  510. <h3>Navigation</h3>
  511. <ul>
  512. <li class="right" style="margin-right: 10px">
  513. <a href="../genindex.html" title="General Index"
  514. >index</a></li>
  515. <li class="right" >
  516. <a href="../py-modindex.html" title="Python Module Index"
  517. >modules</a> |</li>
  518. <li class="right" >
  519. <a href="signal.html" title="signal — Set handlers for asynchronous events"
  520. >next</a> |</li>
  521. <li class="right" >
  522. <a href="select.html" title="select — Waiting for I/O completion"
  523. >previous</a> |</li>
  524. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  525. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  526. <li class="switchers">
  527. <div class="language_switcher_placeholder"></div>
  528. <div class="version_switcher_placeholder"></div>
  529. </li>
  530. <li>
  531. </li>
  532. <li id="cpython-language-and-version">
  533. <a href="../index.html">3.12.3 Documentation</a> &#187;
  534. </li>
  535. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  536. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  537. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">selectors</span></code> — High-level I/O multiplexing</a></li>
  538. <li class="right">
  539. <div class="inline-search" role="search">
  540. <form class="inline-search" action="../search.html" method="get">
  541. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  542. <input type="submit" value="Go" />
  543. </form>
  544. </div>
  545. |
  546. </li>
  547. <li class="right">
  548. <label class="theme-selector-label">
  549. Theme
  550. <select class="theme-selector" oninput="activateTheme(this.value)">
  551. <option value="auto" selected>Auto</option>
  552. <option value="light">Light</option>
  553. <option value="dark">Dark</option>
  554. </select>
  555. </label> |</li>
  556. </ul>
  557. </div>
  558. <div class="footer">
  559. &copy;
  560. <a href="../copyright.html">
  561. Copyright
  562. </a>
  563. 2001-2024, Python Software Foundation.
  564. <br />
  565. This page is licensed under the Python Software Foundation License Version 2.
  566. <br />
  567. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  568. <br />
  569. See <a href="/license.html">History and License</a> for more information.<br />
  570. <br />
  571. The Python Software Foundation is a non-profit corporation.
  572. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  573. <br />
  574. <br />
  575. Last updated on Apr 09, 2024 (13:47 UTC).
  576. <a href="/bugs.html">Found a bug</a>?
  577. <br />
  578. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  579. </div>
  580. </body>
  581. </html>
上海开阖软件有限公司 沪ICP备12045867号-1