gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

613 linhas
46KB

  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="queue — A synchronized queue class" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/queue.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/queue.py The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multip..." />
  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/queue.py The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multip..." />
  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>queue — A synchronized queue class &#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="contextvars — Context Variables" href="contextvars.html" />
  33. <link rel="prev" title="sched — Event scheduler" href="sched.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/queue.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">queue</span></code> — A synchronized queue class</a><ul>
  86. <li><a class="reference internal" href="#queue-objects">Queue Objects</a></li>
  87. <li><a class="reference internal" href="#simplequeue-objects">SimpleQueue Objects</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="sched.html"
  95. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> — Event scheduler</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="contextvars.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">contextvars</span></code> — Context Variables</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/queue.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="contextvars.html" title="contextvars — Context Variables"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="sched.html" title="sched — Event scheduler"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">Concurrent Execution</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code> — A synchronized queue class</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-queue">
  170. <span id="queue-a-synchronized-queue-class"></span><h1><a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> — A synchronized queue class<a class="headerlink" href="#module-queue" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/queue.py">Lib/queue.py</a></p>
  172. <hr class="docutils" />
  173. <p>The <a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> module implements multi-producer, multi-consumer queues.
  174. It is especially useful in threaded programming when information must be
  175. exchanged safely between multiple threads. The <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> class in this
  176. module implements all the required locking semantics.</p>
  177. <p>The module implements three types of queue, which differ only in the order in
  178. which the entries are retrieved. In a <abbr title="first-in, first-out">FIFO</abbr>
  179. queue, the first tasks added are the first retrieved. In a
  180. <abbr title="last-in, first-out">LIFO</abbr> queue, the most recently added entry is
  181. the first retrieved (operating like a stack). With a priority queue,
  182. the entries are kept sorted (using the <a class="reference internal" href="heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">heapq</span></code></a> module) and the
  183. lowest valued entry is retrieved first.</p>
  184. <p>Internally, those three types of queues use locks to temporarily block
  185. competing threads; however, they are not designed to handle reentrancy
  186. within a thread.</p>
  187. <p>In addition, the module implements a “simple”
  188. <abbr title="first-in, first-out">FIFO</abbr> queue type, <a class="reference internal" href="#queue.SimpleQueue" title="queue.SimpleQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleQueue</span></code></a>, whose
  189. specific implementation provides additional guarantees
  190. in exchange for the smaller functionality.</p>
  191. <p>The <a class="reference internal" href="#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> module defines the following classes and exceptions:</p>
  192. <dl class="py class">
  193. <dt class="sig sig-object py" id="queue.Queue">
  194. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">queue.</span></span><span class="sig-name descname"><span class="pre">Queue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">maxsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue" title="Link to this definition">¶</a></dt>
  195. <dd><p>Constructor for a <abbr title="first-in, first-out">FIFO</abbr> queue. <em>maxsize</em> is
  196. an integer that sets the upperbound
  197. limit on the number of items that can be placed in the queue. Insertion will
  198. block once this size has been reached, until queue items are consumed. If
  199. <em>maxsize</em> is less than or equal to zero, the queue size is infinite.</p>
  200. </dd></dl>
  201. <dl class="py class">
  202. <dt class="sig sig-object py" id="queue.LifoQueue">
  203. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">queue.</span></span><span class="sig-name descname"><span class="pre">LifoQueue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">maxsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#queue.LifoQueue" title="Link to this definition">¶</a></dt>
  204. <dd><p>Constructor for a <abbr title="last-in, first-out">LIFO</abbr> queue. <em>maxsize</em> is
  205. an integer that sets the upperbound
  206. limit on the number of items that can be placed in the queue. Insertion will
  207. block once this size has been reached, until queue items are consumed. If
  208. <em>maxsize</em> is less than or equal to zero, the queue size is infinite.</p>
  209. </dd></dl>
  210. <dl class="py class">
  211. <dt class="sig sig-object py" id="queue.PriorityQueue">
  212. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">queue.</span></span><span class="sig-name descname"><span class="pre">PriorityQueue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">maxsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#queue.PriorityQueue" title="Link to this definition">¶</a></dt>
  213. <dd><p>Constructor for a priority queue. <em>maxsize</em> is an integer that sets the upperbound
  214. limit on the number of items that can be placed in the queue. Insertion will
  215. block once this size has been reached, until queue items are consumed. If
  216. <em>maxsize</em> is less than or equal to zero, the queue size is infinite.</p>
  217. <p>The lowest valued entries are retrieved first (the lowest valued entry is the
  218. one that would be returned by <code class="docutils literal notranslate"><span class="pre">min(entries)</span></code>). A typical pattern for
  219. entries is a tuple in the form: <code class="docutils literal notranslate"><span class="pre">(priority_number,</span> <span class="pre">data)</span></code>.</p>
  220. <p>If the <em>data</em> elements are not comparable, the data can be wrapped in a class
  221. that ignores the data item and only compares the priority number:</p>
  222. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">dataclasses</span> <span class="kn">import</span> <span class="n">dataclass</span><span class="p">,</span> <span class="n">field</span>
  223. <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>
  224. <span class="nd">@dataclass</span><span class="p">(</span><span class="n">order</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  225. <span class="k">class</span> <span class="nc">PrioritizedItem</span><span class="p">:</span>
  226. <span class="n">priority</span><span class="p">:</span> <span class="nb">int</span>
  227. <span class="n">item</span><span class="p">:</span> <span class="n">Any</span><span class="o">=</span><span class="n">field</span><span class="p">(</span><span class="n">compare</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  228. </pre></div>
  229. </div>
  230. </dd></dl>
  231. <dl class="py class">
  232. <dt class="sig sig-object py" id="queue.SimpleQueue">
  233. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">queue.</span></span><span class="sig-name descname"><span class="pre">SimpleQueue</span></span><a class="headerlink" href="#queue.SimpleQueue" title="Link to this definition">¶</a></dt>
  234. <dd><p>Constructor for an unbounded <abbr title="first-in, first-out">FIFO</abbr> queue.
  235. Simple queues lack advanced functionality such as task tracking.</p>
  236. <div class="versionadded">
  237. <p><span class="versionmodified added">New in version 3.7.</span></p>
  238. </div>
  239. </dd></dl>
  240. <dl class="py exception">
  241. <dt class="sig sig-object py" id="queue.Empty">
  242. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">queue.</span></span><span class="sig-name descname"><span class="pre">Empty</span></span><a class="headerlink" href="#queue.Empty" title="Link to this definition">¶</a></dt>
  243. <dd><p>Exception raised when non-blocking <a class="reference internal" href="#queue.Queue.get" title="queue.Queue.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> (or
  244. <a class="reference internal" href="#queue.Queue.get_nowait" title="queue.Queue.get_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_nowait()</span></code></a>) is called
  245. on a <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> object which is empty.</p>
  246. </dd></dl>
  247. <dl class="py exception">
  248. <dt class="sig sig-object py" id="queue.Full">
  249. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">queue.</span></span><span class="sig-name descname"><span class="pre">Full</span></span><a class="headerlink" href="#queue.Full" title="Link to this definition">¶</a></dt>
  250. <dd><p>Exception raised when non-blocking <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> (or
  251. <a class="reference internal" href="#queue.Queue.put_nowait" title="queue.Queue.put_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put_nowait()</span></code></a>) is called
  252. on a <a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> object which is full.</p>
  253. </dd></dl>
  254. <section id="queue-objects">
  255. <span id="queueobjects"></span><h2>Queue Objects<a class="headerlink" href="#queue-objects" title="Link to this heading">¶</a></h2>
  256. <p>Queue objects (<a class="reference internal" href="#queue.Queue" title="queue.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a>, <a class="reference internal" href="#queue.LifoQueue" title="queue.LifoQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">LifoQueue</span></code></a>, or <a class="reference internal" href="#queue.PriorityQueue" title="queue.PriorityQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">PriorityQueue</span></code></a>)
  257. provide the public methods described below.</p>
  258. <dl class="py method">
  259. <dt class="sig sig-object py" id="queue.Queue.qsize">
  260. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">qsize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.qsize" title="Link to this definition">¶</a></dt>
  261. <dd><p>Return the approximate size of the queue. Note, qsize() &gt; 0 doesn’t
  262. guarantee that a subsequent get() will not block, nor will qsize() &lt; maxsize
  263. guarantee that put() will not block.</p>
  264. </dd></dl>
  265. <dl class="py method">
  266. <dt class="sig sig-object py" id="queue.Queue.empty">
  267. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">empty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.empty" title="Link to this definition">¶</a></dt>
  268. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the queue is empty, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. If empty()
  269. returns <code class="docutils literal notranslate"><span class="pre">True</span></code> it doesn’t guarantee that a subsequent call to put()
  270. will not block. Similarly, if empty() returns <code class="docutils literal notranslate"><span class="pre">False</span></code> it doesn’t
  271. guarantee that a subsequent call to get() will not block.</p>
  272. </dd></dl>
  273. <dl class="py method">
  274. <dt class="sig sig-object py" id="queue.Queue.full">
  275. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">full</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.full" title="Link to this definition">¶</a></dt>
  276. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the queue is full, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. If full()
  277. returns <code class="docutils literal notranslate"><span class="pre">True</span></code> it doesn’t guarantee that a subsequent call to get()
  278. will not block. Similarly, if full() returns <code class="docutils literal notranslate"><span class="pre">False</span></code> it doesn’t
  279. guarantee that a subsequent call to put() will not block.</p>
  280. </dd></dl>
  281. <dl class="py method">
  282. <dt class="sig sig-object py" id="queue.Queue.put">
  283. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">put</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">item</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">block</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <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="#queue.Queue.put" title="Link to this definition">¶</a></dt>
  284. <dd><p>Put <em>item</em> into the queue. If optional args <em>block</em> is true and <em>timeout</em> is
  285. <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), block if necessary until a free slot is available. If
  286. <em>timeout</em> is a positive number, it blocks at most <em>timeout</em> seconds and raises
  287. the <a class="reference internal" href="#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Full</span></code></a> exception if no free slot was available within that time.
  288. Otherwise (<em>block</em> is false), put an item on the queue if a free slot is
  289. immediately available, else raise the <a class="reference internal" href="#queue.Full" title="queue.Full"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Full</span></code></a> exception (<em>timeout</em> is
  290. ignored in that case).</p>
  291. </dd></dl>
  292. <dl class="py method">
  293. <dt class="sig sig-object py" id="queue.Queue.put_nowait">
  294. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">put_nowait</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">item</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.put_nowait" title="Link to this definition">¶</a></dt>
  295. <dd><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">put(item,</span> <span class="pre">block=False)</span></code>.</p>
  296. </dd></dl>
  297. <dl class="py method">
  298. <dt class="sig sig-object py" id="queue.Queue.get">
  299. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">block</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <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="#queue.Queue.get" title="Link to this definition">¶</a></dt>
  300. <dd><p>Remove and return an item from the queue. If optional args <em>block</em> is true and
  301. <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), block if necessary until an item is available.
  302. If <em>timeout</em> is a positive number, it blocks at most <em>timeout</em> seconds and
  303. raises the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> exception if no item was available within that time.
  304. Otherwise (<em>block</em> is false), return an item if one is immediately available,
  305. else raise the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> exception (<em>timeout</em> is ignored in that case).</p>
  306. <p>Prior to 3.0 on POSIX systems, and for all versions on Windows, if
  307. <em>block</em> is true and <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, this operation goes into
  308. an uninterruptible wait on an underlying lock. This means that no exceptions
  309. can occur, and in particular a SIGINT will not trigger a <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a>.</p>
  310. </dd></dl>
  311. <dl class="py method">
  312. <dt class="sig sig-object py" id="queue.Queue.get_nowait">
  313. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">get_nowait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.get_nowait" title="Link to this definition">¶</a></dt>
  314. <dd><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">get(False)</span></code>.</p>
  315. </dd></dl>
  316. <p>Two methods are offered to support tracking whether enqueued tasks have been
  317. fully processed by daemon consumer threads.</p>
  318. <dl class="py method">
  319. <dt class="sig sig-object py" id="queue.Queue.task_done">
  320. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">task_done</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.task_done" title="Link to this definition">¶</a></dt>
  321. <dd><p>Indicate that a formerly enqueued task is complete. Used by queue consumer
  322. threads. For each <a class="reference internal" href="#queue.Queue.get" title="queue.Queue.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> used to fetch a task, a subsequent call to
  323. <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> tells the queue that the processing on the task is complete.</p>
  324. <p>If a <a class="reference internal" href="#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> is currently blocking, it will resume when all items have been
  325. processed (meaning that a <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> call was received for every item
  326. that had been <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">put()</span></code></a> into the queue).</p>
  327. <p>Raises a <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 called more times than there were items placed in
  328. the queue.</p>
  329. </dd></dl>
  330. <dl class="py method">
  331. <dt class="sig sig-object py" id="queue.Queue.join">
  332. <span class="sig-prename descclassname"><span class="pre">Queue.</span></span><span class="sig-name descname"><span class="pre">join</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.Queue.join" title="Link to this definition">¶</a></dt>
  333. <dd><p>Blocks until all items in the queue have been gotten and processed.</p>
  334. <p>The count of unfinished tasks goes up whenever an item is added to the queue.
  335. The count goes down whenever a consumer thread calls <a class="reference internal" href="#queue.Queue.task_done" title="queue.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">task_done()</span></code></a> to
  336. indicate that the item was retrieved and all work on it is complete. When the
  337. count of unfinished tasks drops to zero, <a class="reference internal" href="#queue.Queue.join" title="queue.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> unblocks.</p>
  338. </dd></dl>
  339. <p>Example of how to wait for enqueued tasks to be completed:</p>
  340. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">threading</span>
  341. <span class="kn">import</span> <span class="nn">queue</span>
  342. <span class="n">q</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
  343. <span class="k">def</span> <span class="nf">worker</span><span class="p">():</span>
  344. <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  345. <span class="n">item</span> <span class="o">=</span> <span class="n">q</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
  346. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Working on </span><span class="si">{</span><span class="n">item</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
  347. <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Finished </span><span class="si">{</span><span class="n">item</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
  348. <span class="n">q</span><span class="o">.</span><span class="n">task_done</span><span class="p">()</span>
  349. <span class="c1"># Turn-on the worker thread.</span>
  350. <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">worker</span><span class="p">,</span> <span class="n">daemon</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  351. <span class="c1"># Send thirty task requests to the worker.</span>
  352. <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">):</span>
  353. <span class="n">q</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
  354. <span class="c1"># Block until all tasks are done.</span>
  355. <span class="n">q</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  356. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;All work completed&#39;</span><span class="p">)</span>
  357. </pre></div>
  358. </div>
  359. </section>
  360. <section id="simplequeue-objects">
  361. <h2>SimpleQueue Objects<a class="headerlink" href="#simplequeue-objects" title="Link to this heading">¶</a></h2>
  362. <p><a class="reference internal" href="#queue.SimpleQueue" title="queue.SimpleQueue"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleQueue</span></code></a> objects provide the public methods described below.</p>
  363. <dl class="py method">
  364. <dt class="sig sig-object py" id="queue.SimpleQueue.qsize">
  365. <span class="sig-prename descclassname"><span class="pre">SimpleQueue.</span></span><span class="sig-name descname"><span class="pre">qsize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.qsize" title="Link to this definition">¶</a></dt>
  366. <dd><p>Return the approximate size of the queue. Note, qsize() &gt; 0 doesn’t
  367. guarantee that a subsequent get() will not block.</p>
  368. </dd></dl>
  369. <dl class="py method">
  370. <dt class="sig sig-object py" id="queue.SimpleQueue.empty">
  371. <span class="sig-prename descclassname"><span class="pre">SimpleQueue.</span></span><span class="sig-name descname"><span class="pre">empty</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.empty" title="Link to this definition">¶</a></dt>
  372. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the queue is empty, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. If empty()
  373. returns <code class="docutils literal notranslate"><span class="pre">False</span></code> it doesn’t guarantee that a subsequent call to get()
  374. will not block.</p>
  375. </dd></dl>
  376. <dl class="py method">
  377. <dt class="sig sig-object py" id="queue.SimpleQueue.put">
  378. <span class="sig-prename descclassname"><span class="pre">SimpleQueue.</span></span><span class="sig-name descname"><span class="pre">put</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">item</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">block</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <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="#queue.SimpleQueue.put" title="Link to this definition">¶</a></dt>
  379. <dd><p>Put <em>item</em> into the queue. The method never blocks and always succeeds
  380. (except for potential low-level errors such as failure to allocate memory).
  381. The optional args <em>block</em> and <em>timeout</em> are ignored and only provided
  382. for compatibility with <a class="reference internal" href="#queue.Queue.put" title="queue.Queue.put"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.put()</span></code></a>.</p>
  383. <div class="impl-detail compound">
  384. <p><strong>CPython implementation detail:</strong> This method has a C implementation which is reentrant. That is, a
  385. <code class="docutils literal notranslate"><span class="pre">put()</span></code> or <code class="docutils literal notranslate"><span class="pre">get()</span></code> call can be interrupted by another <code class="docutils literal notranslate"><span class="pre">put()</span></code>
  386. call in the same thread without deadlocking or corrupting internal
  387. state inside the queue. This makes it appropriate for use in
  388. destructors such as <code class="docutils literal notranslate"><span class="pre">__del__</span></code> methods or <a class="reference internal" href="weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> callbacks.</p>
  389. </div>
  390. </dd></dl>
  391. <dl class="py method">
  392. <dt class="sig sig-object py" id="queue.SimpleQueue.put_nowait">
  393. <span class="sig-prename descclassname"><span class="pre">SimpleQueue.</span></span><span class="sig-name descname"><span class="pre">put_nowait</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">item</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.put_nowait" title="Link to this definition">¶</a></dt>
  394. <dd><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">put(item,</span> <span class="pre">block=False)</span></code>, provided for compatibility with
  395. <a class="reference internal" href="#queue.Queue.put_nowait" title="queue.Queue.put_nowait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.put_nowait()</span></code></a>.</p>
  396. </dd></dl>
  397. <dl class="py method">
  398. <dt class="sig sig-object py" id="queue.SimpleQueue.get">
  399. <span class="sig-prename descclassname"><span class="pre">SimpleQueue.</span></span><span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">block</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <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="#queue.SimpleQueue.get" title="Link to this definition">¶</a></dt>
  400. <dd><p>Remove and return an item from the queue. If optional args <em>block</em> is true and
  401. <em>timeout</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), block if necessary until an item is available.
  402. If <em>timeout</em> is a positive number, it blocks at most <em>timeout</em> seconds and
  403. raises the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> exception if no item was available within that time.
  404. Otherwise (<em>block</em> is false), return an item if one is immediately available,
  405. else raise the <a class="reference internal" href="#queue.Empty" title="queue.Empty"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Empty</span></code></a> exception (<em>timeout</em> is ignored in that case).</p>
  406. </dd></dl>
  407. <dl class="py method">
  408. <dt class="sig sig-object py" id="queue.SimpleQueue.get_nowait">
  409. <span class="sig-prename descclassname"><span class="pre">SimpleQueue.</span></span><span class="sig-name descname"><span class="pre">get_nowait</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#queue.SimpleQueue.get_nowait" title="Link to this definition">¶</a></dt>
  410. <dd><p>Equivalent to <code class="docutils literal notranslate"><span class="pre">get(False)</span></code>.</p>
  411. </dd></dl>
  412. <div class="admonition seealso">
  413. <p class="admonition-title">See also</p>
  414. <dl class="simple">
  415. <dt>Class <a class="reference internal" href="multiprocessing.html#multiprocessing.Queue" title="multiprocessing.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.Queue</span></code></a></dt><dd><p>A queue class for use in a multi-processing (rather than multi-threading)
  416. context.</p>
  417. </dd>
  418. </dl>
  419. <p><a class="reference internal" href="collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.deque</span></code></a> is an alternative implementation of unbounded
  420. queues with fast atomic <a class="reference internal" href="collections.html#collections.deque.append" title="collections.deque.append"><code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code></a> and
  421. <a class="reference internal" href="collections.html#collections.deque.popleft" title="collections.deque.popleft"><code class="xref py py-meth docutils literal notranslate"><span class="pre">popleft()</span></code></a> operations that do not require locking
  422. and also support indexing.</p>
  423. </div>
  424. </section>
  425. </section>
  426. <div class="clearer"></div>
  427. </div>
  428. </div>
  429. </div>
  430. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  431. <div class="sphinxsidebarwrapper">
  432. <div>
  433. <h3><a href="../contents.html">Table of Contents</a></h3>
  434. <ul>
  435. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code> — A synchronized queue class</a><ul>
  436. <li><a class="reference internal" href="#queue-objects">Queue Objects</a></li>
  437. <li><a class="reference internal" href="#simplequeue-objects">SimpleQueue Objects</a></li>
  438. </ul>
  439. </li>
  440. </ul>
  441. </div>
  442. <div>
  443. <h4>Previous topic</h4>
  444. <p class="topless"><a href="sched.html"
  445. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sched</span></code> — Event scheduler</a></p>
  446. </div>
  447. <div>
  448. <h4>Next topic</h4>
  449. <p class="topless"><a href="contextvars.html"
  450. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">contextvars</span></code> — Context Variables</a></p>
  451. </div>
  452. <div role="note" aria-label="source link">
  453. <h3>This Page</h3>
  454. <ul class="this-page-menu">
  455. <li><a href="../bugs.html">Report a Bug</a></li>
  456. <li>
  457. <a href="https://github.com/python/cpython/blob/main/Doc/library/queue.rst"
  458. rel="nofollow">Show Source
  459. </a>
  460. </li>
  461. </ul>
  462. </div>
  463. </div>
  464. <div id="sidebarbutton" title="Collapse sidebar">
  465. <span>«</span>
  466. </div>
  467. </div>
  468. <div class="clearer"></div>
  469. </div>
  470. <div class="related" role="navigation" aria-label="related navigation">
  471. <h3>Navigation</h3>
  472. <ul>
  473. <li class="right" style="margin-right: 10px">
  474. <a href="../genindex.html" title="General Index"
  475. >index</a></li>
  476. <li class="right" >
  477. <a href="../py-modindex.html" title="Python Module Index"
  478. >modules</a> |</li>
  479. <li class="right" >
  480. <a href="contextvars.html" title="contextvars — Context Variables"
  481. >next</a> |</li>
  482. <li class="right" >
  483. <a href="sched.html" title="sched — Event scheduler"
  484. >previous</a> |</li>
  485. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  486. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  487. <li class="switchers">
  488. <div class="language_switcher_placeholder"></div>
  489. <div class="version_switcher_placeholder"></div>
  490. </li>
  491. <li>
  492. </li>
  493. <li id="cpython-language-and-version">
  494. <a href="../index.html">3.12.3 Documentation</a> &#187;
  495. </li>
  496. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  497. <li class="nav-item nav-item-2"><a href="concurrency.html" >Concurrent Execution</a> &#187;</li>
  498. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code> — A synchronized queue class</a></li>
  499. <li class="right">
  500. <div class="inline-search" role="search">
  501. <form class="inline-search" action="../search.html" method="get">
  502. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  503. <input type="submit" value="Go" />
  504. </form>
  505. </div>
  506. |
  507. </li>
  508. <li class="right">
  509. <label class="theme-selector-label">
  510. Theme
  511. <select class="theme-selector" oninput="activateTheme(this.value)">
  512. <option value="auto" selected>Auto</option>
  513. <option value="light">Light</option>
  514. <option value="dark">Dark</option>
  515. </select>
  516. </label> |</li>
  517. </ul>
  518. </div>
  519. <div class="footer">
  520. &copy;
  521. <a href="../copyright.html">
  522. Copyright
  523. </a>
  524. 2001-2024, Python Software Foundation.
  525. <br />
  526. This page is licensed under the Python Software Foundation License Version 2.
  527. <br />
  528. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  529. <br />
  530. See <a href="/license.html">History and License</a> for more information.<br />
  531. <br />
  532. The Python Software Foundation is a non-profit corporation.
  533. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  534. <br />
  535. <br />
  536. Last updated on Apr 09, 2024 (13:47 UTC).
  537. <a href="/bugs.html">Found a bug</a>?
  538. <br />
  539. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  540. </div>
  541. </body>
  542. </html>
上海开阖软件有限公司 沪ICP备12045867号-1