CINXE.COM

threading — Thread-based parallelism — Python 3.13.1 documentation

<!DOCTYPE html> <html lang="en" data-content_root="../"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" /> <meta property="og:title" content="threading — Thread-based parallelism" /> <meta property="og:type" content="website" /> <meta property="og:url" content="https://docs.python.org/3/library/threading.html" /> <meta property="og:site_name" content="Python documentation" /> <meta property="og:description" content="Source code: Lib/threading.py This module constructs higher-level threading interfaces on top of the lower level_thread module. CPython implementation detail: In CPython, due to the Global Interpre..." /> <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" /> <meta property="og:image:alt" content="Python documentation" /> <meta name="description" content="Source code: Lib/threading.py This module constructs higher-level threading interfaces on top of the lower level_thread module. CPython implementation detail: In CPython, due to the Global Interpre..." /> <meta property="og:image:width" content="200" /> <meta property="og:image:height" content="200" /> <meta name="theme-color" content="#3776ab" /> <title>threading — Thread-based parallelism &#8212; Python 3.13.1 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" /> <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=41b4f12d" /> <link rel="stylesheet" type="text/css" href="../_static/sidebar-wrap.css?v=65806c5a" /> <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" /> <script src="../_static/documentation_options.js?v=ced7c45e"></script> <script src="../_static/doctools.js?v=9bcbadda"></script> <script src="../_static/sphinx_highlight.js?v=dc90522c"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.13.1 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="multiprocessing — Process-based parallelism" href="multiprocessing.html" /> <link rel="prev" title="Concurrent Execution" href="concurrency.html" /> <script defer data-domain="docs.python.org" src="https://plausible.io/js/script.js"></script> <link rel="canonical" href="https://docs.python.org/3/library/threading.html" /> <style> @media only screen { table.full-width-table { width: 100%; } } </style> <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css"> <link rel="shortcut icon" type="image/png" href="../_static/py.svg" /> <script type="text/javascript" src="../_static/copybutton.js"></script> <script type="text/javascript" src="../_static/menu.js"></script> <script type="text/javascript" src="../_static/search-focus.js"></script> <script type="text/javascript" src="../_static/themetoggle.js"></script> <script type="text/javascript" src="../_static/rtd_switcher.js"></script> <meta name="readthedocs-addons-api-version" content="1"> </head> <body> <div class="mobile-nav"> <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation" aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" /> <nav class="nav-content" role="navigation"> <label for="menuToggler" class="toggler__label"> <span></span> </label> <span class="nav-items-wrapper"> <a href="https://www.python.org/" class="nav-logo"> <img src="../_static/py.svg" alt="Python logo"/> </a> <span class="version_switcher_placeholder"></span> <form role="search" class="search" action="../search.html" method="get"> <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon"> <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> </svg> <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" /> <input type="submit" value="Go"/> </form> </span> </nav> <div class="menu-wrapper"> <nav class="menu" role="navigation" aria-label="main navigation"> <div class="language_switcher_placeholder"></div> <label class="theme-selector-label"> Theme <select class="theme-selector" oninput="activateTheme(this.value)"> <option value="auto" selected>Auto</option> <option value="light">Light</option> <option value="dark">Dark</option> </select> </label> <div> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> — Thread-based parallelism</a><ul> <li><a class="reference internal" href="#threading.active_count"><code class="docutils literal notranslate"><span class="pre">active_count()</span></code></a></li> <li><a class="reference internal" href="#threading.current_thread"><code class="docutils literal notranslate"><span class="pre">current_thread()</span></code></a></li> <li><a class="reference internal" href="#threading.excepthook"><code class="docutils literal notranslate"><span class="pre">excepthook()</span></code></a></li> <li><a class="reference internal" href="#threading.__excepthook__"><code class="docutils literal notranslate"><span class="pre">__excepthook__</span></code></a></li> <li><a class="reference internal" href="#threading.get_ident"><code class="docutils literal notranslate"><span class="pre">get_ident()</span></code></a></li> <li><a class="reference internal" href="#threading.get_native_id"><code class="docutils literal notranslate"><span class="pre">get_native_id()</span></code></a></li> <li><a class="reference internal" href="#threading.enumerate"><code class="docutils literal notranslate"><span class="pre">enumerate()</span></code></a></li> <li><a class="reference internal" href="#threading.main_thread"><code class="docutils literal notranslate"><span class="pre">main_thread()</span></code></a></li> <li><a class="reference internal" href="#threading.settrace"><code class="docutils literal notranslate"><span class="pre">settrace()</span></code></a></li> <li><a class="reference internal" href="#threading.settrace_all_threads"><code class="docutils literal notranslate"><span class="pre">settrace_all_threads()</span></code></a></li> <li><a class="reference internal" href="#threading.gettrace"><code class="docutils literal notranslate"><span class="pre">gettrace()</span></code></a></li> <li><a class="reference internal" href="#threading.setprofile"><code class="docutils literal notranslate"><span class="pre">setprofile()</span></code></a></li> <li><a class="reference internal" href="#threading.setprofile_all_threads"><code class="docutils literal notranslate"><span class="pre">setprofile_all_threads()</span></code></a></li> <li><a class="reference internal" href="#threading.getprofile"><code class="docutils literal notranslate"><span class="pre">getprofile()</span></code></a></li> <li><a class="reference internal" href="#threading.stack_size"><code class="docutils literal notranslate"><span class="pre">stack_size()</span></code></a></li> <li><a class="reference internal" href="#threading.TIMEOUT_MAX"><code class="docutils literal notranslate"><span class="pre">TIMEOUT_MAX</span></code></a></li> <li><a class="reference internal" href="#thread-local-data">Thread-Local Data</a><ul> <li><a class="reference internal" href="#threading.local"><code class="docutils literal notranslate"><span class="pre">local</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#thread-objects">Thread Objects</a><ul> <li><a class="reference internal" href="#threading.Thread"><code class="docutils literal notranslate"><span class="pre">Thread</span></code></a><ul> <li><a class="reference internal" href="#threading.Thread.start"><code class="docutils literal notranslate"><span class="pre">start()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.run"><code class="docutils literal notranslate"><span class="pre">run()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.join"><code class="docutils literal notranslate"><span class="pre">join()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.name"><code class="docutils literal notranslate"><span class="pre">name</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.getName"><code class="docutils literal notranslate"><span class="pre">getName()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.setName"><code class="docutils literal notranslate"><span class="pre">setName()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.ident"><code class="docutils literal notranslate"><span class="pre">ident</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.native_id"><code class="docutils literal notranslate"><span class="pre">native_id</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.is_alive"><code class="docutils literal notranslate"><span class="pre">is_alive()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.daemon"><code class="docutils literal notranslate"><span class="pre">daemon</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.isDaemon"><code class="docutils literal notranslate"><span class="pre">isDaemon()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.setDaemon"><code class="docutils literal notranslate"><span class="pre">setDaemon()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#lock-objects">Lock Objects</a><ul> <li><a class="reference internal" href="#threading.Lock"><code class="docutils literal notranslate"><span class="pre">Lock</span></code></a><ul> <li><a class="reference internal" href="#threading.Lock.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.Lock.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> <li><a class="reference internal" href="#threading.Lock.locked"><code class="docutils literal notranslate"><span class="pre">locked()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#rlock-objects">RLock Objects</a><ul> <li><a class="reference internal" href="#threading.RLock"><code class="docutils literal notranslate"><span class="pre">RLock</span></code></a><ul> <li><a class="reference internal" href="#threading.RLock.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.RLock.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#condition-objects">Condition Objects</a><ul> <li><a class="reference internal" href="#threading.Condition"><code class="docutils literal notranslate"><span class="pre">Condition</span></code></a><ul> <li><a class="reference internal" href="#threading.Condition.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.wait"><code class="docutils literal notranslate"><span class="pre">wait()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.wait_for"><code class="docutils literal notranslate"><span class="pre">wait_for()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.notify"><code class="docutils literal notranslate"><span class="pre">notify()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.notify_all"><code class="docutils literal notranslate"><span class="pre">notify_all()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#semaphore-objects">Semaphore Objects</a><ul> <li><a class="reference internal" href="#threading.Semaphore"><code class="docutils literal notranslate"><span class="pre">Semaphore</span></code></a><ul> <li><a class="reference internal" href="#threading.Semaphore.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.Semaphore.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#threading.BoundedSemaphore"><code class="docutils literal notranslate"><span class="pre">BoundedSemaphore</span></code></a></li> <li><a class="reference internal" href="#semaphore-example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code> Example</a></li> </ul> </li> <li><a class="reference internal" href="#event-objects">Event Objects</a><ul> <li><a class="reference internal" href="#threading.Event"><code class="docutils literal notranslate"><span class="pre">Event</span></code></a><ul> <li><a class="reference internal" href="#threading.Event.is_set"><code class="docutils literal notranslate"><span class="pre">is_set()</span></code></a></li> <li><a class="reference internal" href="#threading.Event.set"><code class="docutils literal notranslate"><span class="pre">set()</span></code></a></li> <li><a class="reference internal" href="#threading.Event.clear"><code class="docutils literal notranslate"><span class="pre">clear()</span></code></a></li> <li><a class="reference internal" href="#threading.Event.wait"><code class="docutils literal notranslate"><span class="pre">wait()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#timer-objects">Timer Objects</a><ul> <li><a class="reference internal" href="#threading.Timer"><code class="docutils literal notranslate"><span class="pre">Timer</span></code></a><ul> <li><a class="reference internal" href="#threading.Timer.cancel"><code class="docutils literal notranslate"><span class="pre">cancel()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#barrier-objects">Barrier Objects</a><ul> <li><a class="reference internal" href="#threading.Barrier"><code class="docutils literal notranslate"><span class="pre">Barrier</span></code></a><ul> <li><a class="reference internal" href="#threading.Barrier.wait"><code class="docutils literal notranslate"><span class="pre">wait()</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.reset"><code class="docutils literal notranslate"><span class="pre">reset()</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.abort"><code class="docutils literal notranslate"><span class="pre">abort()</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.parties"><code class="docutils literal notranslate"><span class="pre">parties</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.n_waiting"><code class="docutils literal notranslate"><span class="pre">n_waiting</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.broken"><code class="docutils literal notranslate"><span class="pre">broken</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#threading.BrokenBarrierError"><code class="docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#using-locks-conditions-and-semaphores-in-the-with-statement">Using locks, conditions, and semaphores in the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement</a></li> </ul> </li> </ul> </div> <div> <h4>Previous topic</h4> <p class="topless"><a href="concurrency.html" title="previous chapter">Concurrent Execution</a></p> </div> <div> <h4>Next topic</h4> <p class="topless"><a href="multiprocessing.html" title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code> — Process-based parallelism</a></p> </div> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/main/Doc/library/threading.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </nav> </div> </div> <div class="related" role="navigation" aria-label="Related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="multiprocessing.html" title="multiprocessing — Process-based parallelism" accesskey="N">next</a> |</li> <li class="right" > <a href="concurrency.html" title="Concurrent Execution" accesskey="P">previous</a> |</li> <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> &#187;</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.13.1 Documentation</a> &#187; </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li> <li class="nav-item nav-item-2"><a href="concurrency.html" accesskey="U">Concurrent Execution</a> &#187;</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> — Thread-based parallelism</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" /> <input type="submit" value="Go" /> </form> </div> | </li> <li class="right"> <label class="theme-selector-label"> Theme <select class="theme-selector" oninput="activateTheme(this.value)"> <option value="auto" selected>Auto</option> <option value="light">Light</option> <option value="dark">Dark</option> </select> </label> |</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="module-threading"> <span id="threading-thread-based-parallelism"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> — Thread-based parallelism<a class="headerlink" href="#module-threading" title="Link to this heading">¶</a></h1> <p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/3.13/Lib/threading.py">Lib/threading.py</a></p> <hr class="docutils" /> <p>This module constructs higher-level threading interfaces on top of the lower level <a class="reference internal" href="_thread.html#module-_thread" title="_thread: Low-level threading API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">_thread</span></code></a> module.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>This module used to be optional, it is now always available.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ThreadPoolExecutor</span></code></a> offers a higher level interface to push tasks to a background thread without blocking execution of the calling thread, while still being able to retrieve their results when needed.</p> <p><a class="reference internal" href="queue.html#module-queue" title="queue: A synchronized queue class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a> provides a thread-safe interface for exchanging data between running threads.</p> <p><a class="reference internal" href="asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a> offers an alternative approach to achieving task level concurrency without requiring the use of multiple operating system threads.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>In the Python 2.x series, this module contained <code class="docutils literal notranslate"><span class="pre">camelCase</span></code> names for some methods and functions. These are deprecated as of Python 3.10, but they are still supported for compatibility with Python 2.5 and lower.</p> </div> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> In CPython, due to the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">Global Interpreter Lock</span></a>, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use <a class="reference internal" href="multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> or <a class="reference internal" href="concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ProcessPoolExecutor</span></code></a>. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p> <p>This module does not work or is not available on WebAssembly. See <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> <p>This module defines the following functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="threading.active_count"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">active_count</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.active_count" title="Link to this definition">¶</a></dt> <dd><p>Return the number of <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> objects currently alive. The returned count is equal to the length of the list returned by <a class="reference internal" href="#threading.enumerate" title="threading.enumerate"><code class="xref py py-func docutils literal notranslate"><span class="pre">enumerate()</span></code></a>.</p> <p>The function <code class="docutils literal notranslate"><span class="pre">activeCount</span></code> is a deprecated alias for this function.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.current_thread"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">current_thread</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.current_thread" title="Link to this definition">¶</a></dt> <dd><p>Return the current <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> object, corresponding to the caller’s thread of control. If the caller’s thread of control was not created through the <a class="reference internal" href="#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module, a dummy thread object with limited functionality is returned.</p> <p>The function <code class="docutils literal notranslate"><span class="pre">currentThread</span></code> is a deprecated alias for this function.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.excepthook"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">excepthook</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.excepthook" title="Link to this definition">¶</a></dt> <dd><p>Handle uncaught exception raised by <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">Thread.run()</span></code></a>.</p> <p>The <em>args</em> argument has the following attributes:</p> <ul class="simple"> <li><p><em>exc_type</em>: Exception type.</p></li> <li><p><em>exc_value</em>: Exception value, can be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li> <li><p><em>exc_traceback</em>: Exception traceback, can be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li> <li><p><em>thread</em>: Thread which raised the exception, can be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li> </ul> <p>If <em>exc_type</em> is <a class="reference internal" href="exceptions.html#SystemExit" title="SystemExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemExit</span></code></a>, the exception is silently ignored. Otherwise, the exception is printed out on <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>.</p> <p>If this function raises an exception, <a class="reference internal" href="sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.excepthook()</span></code></a> is called to handle it.</p> <p><a class="reference internal" href="#threading.excepthook" title="threading.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.excepthook()</span></code></a> can be overridden to control how uncaught exceptions raised by <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">Thread.run()</span></code></a> are handled.</p> <p>Storing <em>exc_value</em> using a custom hook can create a reference cycle. It should be cleared explicitly to break the reference cycle when the exception is no longer needed.</p> <p>Storing <em>thread</em> using a custom hook can resurrect it if it is set to an object which is being finalized. Avoid storing <em>thread</em> after the custom hook completes to avoid resurrecting objects.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.excepthook()</span></code></a> handles uncaught exceptions.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.8.</span></p> </div> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="threading.__excepthook__"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">__excepthook__</span></span><a class="headerlink" href="#threading.__excepthook__" title="Link to this definition">¶</a></dt> <dd><p>Holds the original value of <a class="reference internal" href="#threading.excepthook" title="threading.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.excepthook()</span></code></a>. It is saved so that the original value can be restored in case they happen to get replaced with broken or alternative objects.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.10.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.get_ident"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">get_ident</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.get_ident" title="Link to this definition">¶</a></dt> <dd><p>Return the ‘thread identifier’ of the current thread. This is a nonzero integer. Its value has no direct meaning; it is intended as a magic cookie to be used e.g. to index a dictionary of thread-specific data. Thread identifiers may be recycled when a thread exits and another thread is created.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.3.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.get_native_id"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">get_native_id</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.get_native_id" title="Link to this definition">¶</a></dt> <dd><p>Return the native integral Thread ID of the current thread assigned by the kernel. This is a non-negative integer. Its value may be used to uniquely identify this particular thread system-wide (until the thread terminates, after which the value may be recycled by the OS).</p> <div class="availability docutils container"> <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX, DragonFlyBSD, GNU/kFreeBSD.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.8.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.13: </span>Added support for GNU/kFreeBSD.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.enumerate"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">enumerate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.enumerate" title="Link to this definition">¶</a></dt> <dd><p>Return a list of all <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> objects currently active. The list includes daemonic threads and dummy thread objects created by <a class="reference internal" href="#threading.current_thread" title="threading.current_thread"><code class="xref py py-func docutils literal notranslate"><span class="pre">current_thread()</span></code></a>. It excludes terminated threads and threads that have not yet been started. However, the main thread is always part of the result, even when terminated.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.main_thread"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">main_thread</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.main_thread" title="Link to this definition">¶</a></dt> <dd><p>Return the main <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> object. In normal conditions, the main thread is the thread from which the Python interpreter was started.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.4.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.settrace"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">settrace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.settrace" title="Link to this definition">¶</a></dt> <dd><p id="index-0">Set a trace function for all threads started from the <a class="reference internal" href="#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module. The <em>func</em> will be passed to <a class="reference internal" href="sys.html#sys.settrace" title="sys.settrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.settrace()</span></code></a> for each thread, before its <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method is called.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.settrace_all_threads"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">settrace_all_threads</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.settrace_all_threads" title="Link to this definition">¶</a></dt> <dd><p>Set a trace function for all threads started from the <a class="reference internal" href="#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module and all Python threads that are currently executing.</p> <p>The <em>func</em> will be passed to <a class="reference internal" href="sys.html#sys.settrace" title="sys.settrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.settrace()</span></code></a> for each thread, before its <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method is called.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.12.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.gettrace"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">gettrace</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.gettrace" title="Link to this definition">¶</a></dt> <dd><p id="index-1">Get the trace function as set by <a class="reference internal" href="#threading.settrace" title="threading.settrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">settrace()</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.10.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.setprofile"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">setprofile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.setprofile" title="Link to this definition">¶</a></dt> <dd><p id="index-2">Set a profile function for all threads started from the <a class="reference internal" href="#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module. The <em>func</em> will be passed to <a class="reference internal" href="sys.html#sys.setprofile" title="sys.setprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setprofile()</span></code></a> for each thread, before its <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method is called.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.setprofile_all_threads"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">setprofile_all_threads</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.setprofile_all_threads" title="Link to this definition">¶</a></dt> <dd><p>Set a profile function for all threads started from the <a class="reference internal" href="#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module and all Python threads that are currently executing.</p> <p>The <em>func</em> will be passed to <a class="reference internal" href="sys.html#sys.setprofile" title="sys.setprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setprofile()</span></code></a> for each thread, before its <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method is called.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.12.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.getprofile"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">getprofile</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.getprofile" title="Link to this definition">¶</a></dt> <dd><p id="index-3">Get the profiler function as set by <a class="reference internal" href="#threading.setprofile" title="threading.setprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">setprofile()</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.10.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="threading.stack_size"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">stack_size</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">size</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.stack_size" title="Link to this definition">¶</a></dt> <dd><p>Return the thread stack size used when creating new threads. The optional <em>size</em> argument specifies the stack size to be used for subsequently created threads, and must be 0 (use platform or configured default) or a positive integer value of at least 32,768 (32 KiB). If <em>size</em> is not specified, 0 is used. If changing the thread stack size is unsupported, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised. If the specified stack size is invalid, 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> is raised and the stack size is unmodified. 32 KiB is currently the minimum supported stack size value to guarantee sufficient stack space for the interpreter itself. Note that some platforms may have particular restrictions on values for the stack size, such as requiring a minimum stack size &gt; 32 KiB or requiring allocation in multiples of the system memory page size - platform documentation should be referred to for more information (4 KiB pages are common; using multiples of 4096 for the stack size is the suggested approach in the absence of more specific information).</p> <div class="availability docutils container"> <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows, pthreads.</p> <p>Unix platforms with POSIX threads support.</p> </div> </dd></dl> <p>This module also defines the following constant:</p> <dl class="py data"> <dt class="sig sig-object py" id="threading.TIMEOUT_MAX"> <span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">TIMEOUT_MAX</span></span><a class="headerlink" href="#threading.TIMEOUT_MAX" title="Link to this definition">¶</a></dt> <dd><p>The maximum value allowed for the <em>timeout</em> parameter of blocking functions (<a class="reference internal" href="#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Lock.acquire()</span></code></a>, <a class="reference internal" href="#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">RLock.acquire()</span></code></a>, <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Condition.wait()</span></code></a>, etc.). Specifying a timeout greater than this value will raise an <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.2.</span></p> </div> </dd></dl> <p>This module defines a number of classes, which are detailed in the sections below.</p> <p>The design of this module is loosely based on Java’s threading model. However, where Java makes locks and condition variables basic behavior of every object, they are separate objects in Python. Python’s <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> class supports a subset of the behavior of Java’s Thread class; currently, there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted. The static methods of Java’s Thread class, when implemented, are mapped to module-level functions.</p> <p>All of the methods described below are executed atomically.</p> <section id="thread-local-data"> <h2>Thread-Local Data<a class="headerlink" href="#thread-local-data" title="Link to this heading">¶</a></h2> <p>Thread-local data is data whose values are thread specific. To manage thread-local data, just create an instance of <a class="reference internal" href="#threading.local" title="threading.local"><code class="xref py py-class docutils literal notranslate"><span class="pre">local</span></code></a> (or a subclass) and store attributes on it:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">mydata</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">local</span><span class="p">()</span> <span class="n">mydata</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span> </pre></div> </div> <p>The instance’s values will be different for separate threads.</p> <dl class="py class"> <dt class="sig sig-object py" id="threading.local"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">local</span></span><a class="headerlink" href="#threading.local" title="Link to this definition">¶</a></dt> <dd><p>A class that represents thread-local data.</p> <p>For more details and extensive examples, see the documentation string of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_threading_local</span></code> module: <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/3.13/Lib/_threading_local.py">Lib/_threading_local.py</a>.</p> </dd></dl> </section> <section id="thread-objects"> <span id="id1"></span><h2>Thread Objects<a class="headerlink" href="#thread-objects" title="Link to this heading">¶</a></h2> <p>The <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> class represents an activity that is run in a separate thread of control. There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method in a subclass. No other methods (except for the constructor) should be overridden in a subclass. In other words, <em>only</em> override the <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> and <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> methods of this class.</p> <p>Once a thread object is created, its activity must be started by calling the thread’s <a class="reference internal" href="#threading.Thread.start" title="threading.Thread.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a> method. This invokes the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method in a separate thread of control.</p> <p>Once the thread’s activity is started, the thread is considered ‘alive’. It stops being alive when its <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method terminates – either normally, or by raising an unhandled exception. The <a class="reference internal" href="#threading.Thread.is_alive" title="threading.Thread.is_alive"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_alive()</span></code></a> method tests whether the thread is alive.</p> <p>Other threads can call a thread’s <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> method. This blocks the calling thread until the thread whose <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> method is called is terminated.</p> <p>A thread has a name. The name can be passed to the constructor, and read or changed through the <a class="reference internal" href="#threading.Thread.name" title="threading.Thread.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> attribute.</p> <p>If the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method raises an exception, <a class="reference internal" href="#threading.excepthook" title="threading.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.excepthook()</span></code></a> is called to handle it. By default, <a class="reference internal" href="#threading.excepthook" title="threading.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.excepthook()</span></code></a> ignores silently <a class="reference internal" href="exceptions.html#SystemExit" title="SystemExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemExit</span></code></a>.</p> <p>A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the <a class="reference internal" href="#threading.Thread.daemon" title="threading.Thread.daemon"><code class="xref py py-attr docutils literal notranslate"><span class="pre">daemon</span></code></a> property or the <em>daemon</em> constructor argument.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an <a class="reference internal" href="#threading.Event" title="threading.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">Event</span></code></a>.</p> </div> <p>There is a “main thread” object; this corresponds to the initial thread of control in the Python program. It is not a daemon thread.</p> <p>There is the possibility that “dummy thread objects” are created. These are thread objects corresponding to “alien threads”, which are threads of control started outside the threading module, such as directly from C code. Dummy thread objects have limited functionality; they are always considered alive and daemonic, and cannot be <a class="reference internal" href="#meth-thread-join"><span class="std std-ref">joined</span></a>. They are never deleted, since it is impossible to detect the termination of alien threads.</p> <dl class="py class"> <dt class="sig sig-object py" id="threading.Thread"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">Thread</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">group</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">()</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">daemon</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="#threading.Thread" title="Link to this definition">¶</a></dt> <dd><p>This constructor should always be called with keyword arguments. Arguments are:</p> <p><em>group</em> should be <code class="docutils literal notranslate"><span class="pre">None</span></code>; reserved for future extension when a <code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadGroup</span></code> class is implemented.</p> <p><em>target</em> is the callable object to be invoked by the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method. Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>, meaning nothing is called.</p> <p><em>name</em> is the thread name. By default, a unique name is constructed of the form “Thread-<em>N</em>” where <em>N</em> is a small decimal number, or “Thread-<em>N</em> (target)” where “target” is <code class="docutils literal notranslate"><span class="pre">target.__name__</span></code> if the <em>target</em> argument is specified.</p> <p><em>args</em> is a list or tuple of arguments for the target invocation. Defaults to <code class="docutils literal notranslate"><span class="pre">()</span></code>.</p> <p><em>kwargs</em> is a dictionary of keyword arguments for the target invocation. Defaults to <code class="docutils literal notranslate"><span class="pre">{}</span></code>.</p> <p>If not <code class="docutils literal notranslate"><span class="pre">None</span></code>, <em>daemon</em> explicitly sets whether the thread is daemonic. If <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), the daemonic property is inherited from the current thread.</p> <p>If the subclass overrides the constructor, it must make sure to invoke the base class constructor (<code class="docutils literal notranslate"><span class="pre">Thread.__init__()</span></code>) before doing anything else to the thread.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>daemon</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Use the <em>target</em> name if <em>name</em> argument is omitted.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="threading.Thread.start"> <span class="sig-name descname"><span class="pre">start</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Thread.start" title="Link to this definition">¶</a></dt> <dd><p>Start the thread’s activity.</p> <p>It must be called at most once per thread object. It arranges for the object’s <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method to be invoked in a separate thread of control.</p> <p>This method will raise a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> if called more than once on the same thread object.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Thread.run"> <span class="sig-name descname"><span class="pre">run</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Thread.run" title="Link to this definition">¶</a></dt> <dd><p>Method representing the thread’s activity.</p> <p>You may override this method in a subclass. The standard <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method invokes the callable object passed to the object’s constructor as the <em>target</em> argument, if any, with positional and keyword arguments taken from the <em>args</em> and <em>kwargs</em> arguments, respectively.</p> <p>Using list or tuple as the <em>args</em> argument which passed to the <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> could achieve the same effect.</p> <p>Example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">threading</span> <span class="kn">import</span> <span class="n">Thread</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">t</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="nb">print</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">run</span><span class="p">()</span> <span class="go">1</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">t</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="nb">print</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,))</span> <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">run</span><span class="p">()</span> <span class="go">1</span> </pre></div> </div> </dd></dl> <dl class="py method" id="meth-thread-join"> <dt class="sig sig-object py" id="threading.Thread.join"> <span class="sig-name descname"><span class="pre">join</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="#threading.Thread.join" title="Link to this definition">¶</a></dt> <dd><p>Wait until the thread terminates. This blocks the calling thread until the thread whose <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.</p> <p>When the <em>timeout</em> argument is present and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it should be a floating-point number specifying a timeout for the operation in seconds (or fractions thereof). As <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> always returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, you must call <a class="reference internal" href="#threading.Thread.is_alive" title="threading.Thread.is_alive"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_alive()</span></code></a> after <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> to decide whether a timeout happened – if the thread is still alive, the <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> call timed out.</p> <p>When the <em>timeout</em> argument is not present or <code class="docutils literal notranslate"><span class="pre">None</span></code>, the operation will block until the thread terminates.</p> <p>A thread can be joined many times.</p> <p><a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> raises a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to <a class="reference internal" href="#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">join()</span></code></a> a thread before it has been started and attempts to do so raise the same exception.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="threading.Thread.name"> <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#threading.Thread.name" title="Link to this definition">¶</a></dt> <dd><p>A string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Thread.getName"> <span class="sig-name descname"><span class="pre">getName</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Thread.getName" title="Link to this definition">¶</a></dt> <dt class="sig sig-object py" id="threading.Thread.setName"> <span class="sig-name descname"><span class="pre">setName</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Thread.setName" title="Link to this definition">¶</a></dt> <dd><p>Deprecated getter/setter API for <a class="reference internal" href="#threading.Thread.name" title="threading.Thread.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a>; use it directly as a property instead.</p> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.10.</span></p> </div> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="threading.Thread.ident"> <span class="sig-name descname"><span class="pre">ident</span></span><a class="headerlink" href="#threading.Thread.ident" title="Link to this definition">¶</a></dt> <dd><p>The ‘thread identifier’ of this thread or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the thread has not been started. This is a nonzero integer. See the <a class="reference internal" href="#threading.get_ident" title="threading.get_ident"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_ident()</span></code></a> function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="threading.Thread.native_id"> <span class="sig-name descname"><span class="pre">native_id</span></span><a class="headerlink" href="#threading.Thread.native_id" title="Link to this definition">¶</a></dt> <dd><p>The Thread ID (<code class="docutils literal notranslate"><span class="pre">TID</span></code>) of this thread, as assigned by the OS (kernel). This is a non-negative integer, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the thread has not been started. See the <a class="reference internal" href="#threading.get_native_id" title="threading.get_native_id"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_native_id()</span></code></a> function. This value may be used to uniquely identify this particular thread system-wide (until the thread terminates, after which the value may be recycled by the OS).</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Similar to Process IDs, Thread IDs are only valid (guaranteed unique system-wide) from the time the thread is created until the thread has been terminated.</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX, DragonFlyBSD.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.8.</span></p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Thread.is_alive"> <span class="sig-name descname"><span class="pre">is_alive</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Thread.is_alive" title="Link to this definition">¶</a></dt> <dd><p>Return whether the thread is alive.</p> <p>This method returns <code class="docutils literal notranslate"><span class="pre">True</span></code> just before the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method starts until just after the <a class="reference internal" href="#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> method terminates. The module function <a class="reference internal" href="#threading.enumerate" title="threading.enumerate"><code class="xref py py-func docutils literal notranslate"><span class="pre">enumerate()</span></code></a> returns a list of all alive threads.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="threading.Thread.daemon"> <span class="sig-name descname"><span class="pre">daemon</span></span><a class="headerlink" href="#threading.Thread.daemon" title="Link to this definition">¶</a></dt> <dd><p>A boolean value indicating whether this thread is a daemon thread (<code class="docutils literal notranslate"><span class="pre">True</span></code>) or not (<code class="docutils literal notranslate"><span class="pre">False</span></code>). This must be set before <a class="reference internal" href="#threading.Thread.start" title="threading.Thread.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start()</span></code></a> is called, otherwise <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to <a class="reference internal" href="#threading.Thread.daemon" title="threading.Thread.daemon"><code class="xref py py-attr docutils literal notranslate"><span class="pre">daemon</span></code></a> = <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>The entire Python program exits when no alive non-daemon threads are left.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Thread.isDaemon"> <span class="sig-name descname"><span class="pre">isDaemon</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Thread.isDaemon" title="Link to this definition">¶</a></dt> <dt class="sig sig-object py" id="threading.Thread.setDaemon"> <span class="sig-name descname"><span class="pre">setDaemon</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Thread.setDaemon" title="Link to this definition">¶</a></dt> <dd><p>Deprecated getter/setter API for <a class="reference internal" href="#threading.Thread.daemon" title="threading.Thread.daemon"><code class="xref py py-attr docutils literal notranslate"><span class="pre">daemon</span></code></a>; use it directly as a property instead.</p> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.10.</span></p> </div> </dd></dl> </dd></dl> </section> <section id="lock-objects"> <span id="id2"></span><h2>Lock Objects<a class="headerlink" href="#lock-objects" title="Link to this heading">¶</a></h2> <p>A primitive lock is a synchronization primitive that is not owned by a particular thread when locked. In Python, it is currently the lowest level synchronization primitive available, implemented directly by the <a class="reference internal" href="_thread.html#module-_thread" title="_thread: Low-level threading API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">_thread</span></code></a> extension module.</p> <p>A primitive lock is in one of two states, “locked” or “unlocked”. It is created in the unlocked state. It has two basic methods, <a class="reference internal" href="#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> and <a class="reference internal" href="#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a>. When the state is unlocked, <a class="reference internal" href="#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> changes the state to locked and returns immediately. When the state is locked, <a class="reference internal" href="#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> blocks until a call to <a class="reference internal" href="#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> in another thread changes it to unlocked, then the <a class="reference internal" href="#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> call resets it to locked and returns. The <a class="reference internal" href="#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> will be raised.</p> <p>Locks also support the <a class="reference internal" href="#with-locks"><span class="std std-ref">context management protocol</span></a>.</p> <p>When more than one thread is blocked in <a class="reference internal" href="#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> waiting for the state to turn to unlocked, only one thread proceeds when a <a class="reference internal" href="#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> call resets the state to unlocked; which one of the waiting threads proceeds is not defined, and may vary across implementations.</p> <p>All methods are executed atomically.</p> <dl class="py class"> <dt class="sig sig-object py" id="threading.Lock"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">Lock</span></span><a class="headerlink" href="#threading.Lock" title="Link to this definition">¶</a></dt> <dd><p>The class implementing primitive lock objects. Once a thread has acquired a lock, subsequent attempts to acquire it block, until it is released; any thread may release it.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.13: </span><code class="docutils literal notranslate"><span class="pre">Lock</span></code> is now a class. In earlier Pythons, <code class="docutils literal notranslate"><span class="pre">Lock</span></code> was a factory function which returned an instance of the underlying private lock type.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="threading.Lock.acquire"> <span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">blocking</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">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.Lock.acquire" title="Link to this definition">¶</a></dt> <dd><p>Acquire a lock, blocking or non-blocking.</p> <p>When invoked with the <em>blocking</em> argument set to <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), block until the lock is unlocked, then set it to locked and return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p> <p>When invoked with the <em>blocking</em> argument set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, do not block. If a call with <em>blocking</em> set to <code class="docutils literal notranslate"><span class="pre">True</span></code> would block, return <code class="docutils literal notranslate"><span class="pre">False</span></code> immediately; otherwise, set the lock to locked and return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p> <p>When invoked with the floating-point <em>timeout</em> argument set to a positive value, block for at most the number of seconds specified by <em>timeout</em> and as long as the lock cannot be acquired. A <em>timeout</em> argument of <code class="docutils literal notranslate"><span class="pre">-1</span></code> specifies an unbounded wait. It is forbidden to specify a <em>timeout</em> when <em>blocking</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>The return value is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the lock is acquired successfully, <code class="docutils literal notranslate"><span class="pre">False</span></code> if not (for example if the <em>timeout</em> expired).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>timeout</em> parameter is new.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Lock acquisition can now be interrupted by signals on POSIX if the underlying threading implementation supports it.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Lock.release"> <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Lock.release" title="Link to this definition">¶</a></dt> <dd><p>Release a lock. This can be called from any thread, not only the thread which has acquired the lock.</p> <p>When the lock is locked, reset it to unlocked, and return. If any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.</p> <p>When invoked on an unlocked lock, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p> <p>There is no return value.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Lock.locked"> <span class="sig-name descname"><span class="pre">locked</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Lock.locked" title="Link to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the lock is acquired.</p> </dd></dl> </dd></dl> </section> <section id="rlock-objects"> <span id="id3"></span><h2>RLock Objects<a class="headerlink" href="#rlock-objects" title="Link to this heading">¶</a></h2> <p>A reentrant lock is a synchronization primitive that may be acquired multiple times by the same thread. Internally, it uses the concepts of “owning thread” and “recursion level” in addition to the locked/unlocked state used by primitive locks. In the locked state, some thread owns the lock; in the unlocked state, no thread owns it.</p> <p>Threads call a lock’s <a class="reference internal" href="#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> method to lock it, and its <a class="reference internal" href="#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> method to unlock it.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Reentrant locks support the <a class="reference internal" href="#with-locks"><span class="std std-ref">context management protocol</span></a>, so it is recommended to use <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> instead of manually calling <a class="reference internal" href="#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> and <a class="reference internal" href="#threading.RLock.release" title="threading.RLock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> to handle acquiring and releasing the lock for a block of code.</p> </div> <p>RLock’s <a class="reference internal" href="#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a>/<a class="reference internal" href="#threading.RLock.release" title="threading.RLock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> call pairs may be nested, unlike Lock’s <a class="reference internal" href="#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a>/<a class="reference internal" href="#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a>. Only the final <a class="reference internal" href="#threading.RLock.release" title="threading.RLock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> (the <a class="reference internal" href="#threading.Lock.release" title="threading.Lock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> of the outermost pair) resets the lock to an unlocked state and allows another thread blocked in <a class="reference internal" href="#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> to proceed.</p> <p><a class="reference internal" href="#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a>/<a class="reference internal" href="#threading.RLock.release" title="threading.RLock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> must be used in pairs: each acquire must have a release in the thread that has acquired the lock. Failing to call release as many times the lock has been acquired can lead to deadlock.</p> <dl class="py class"> <dt class="sig sig-object py" id="threading.RLock"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">RLock</span></span><a class="headerlink" href="#threading.RLock" title="Link to this definition">¶</a></dt> <dd><p>This class implements reentrant lock objects. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it.</p> <p>Note that <code class="docutils literal notranslate"><span class="pre">RLock</span></code> is actually a factory function which returns an instance of the most efficient version of the concrete RLock class that is supported by the platform.</p> <dl class="py method"> <dt class="sig sig-object py" id="threading.RLock.acquire"> <span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">blocking</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">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.RLock.acquire" title="Link to this definition">¶</a></dt> <dd><p>Acquire a lock, blocking or non-blocking.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt><a class="reference internal" href="#with-locks"><span class="std std-ref">Using RLock as a context manager</span></a></dt><dd><p>Recommended over manual <code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code> and <a class="reference internal" href="#threading.RLock.release" title="threading.RLock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> calls whenever practical.</p> </dd> </dl> </div> <p>When invoked with the <em>blocking</em> argument set to <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default):</p> <blockquote> <div><ul class="simple"> <li><p>If no thread owns the lock, acquire the lock and return immediately.</p></li> <li><p>If another thread owns the lock, block until we are able to acquire lock, or <em>timeout</em>, if set to a positive float value.</p></li> <li><p>If the same thread owns the lock, acquire the lock again, and return immediately. This is the difference between <a class="reference internal" href="#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> and <code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code>; <a class="reference internal" href="#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> handles this case the same as the previous, blocking until the lock can be acquired.</p></li> </ul> </div></blockquote> <p>When invoked with the <em>blocking</em> argument set to <code class="docutils literal notranslate"><span class="pre">False</span></code>:</p> <blockquote> <div><ul class="simple"> <li><p>If no thread owns the lock, acquire the lock and return immediately.</p></li> <li><p>If another thread owns the lock, return immediately.</p></li> <li><p>If the same thread owns the lock, acquire the lock again and return immediately.</p></li> </ul> </div></blockquote> <p>In all cases, if the thread was able to acquire the lock, return <code class="docutils literal notranslate"><span class="pre">True</span></code>. If the thread was unable to acquire the lock (i.e. if not blocking or the timeout was reached) return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <p>If called multiple times, failing to call <a class="reference internal" href="#threading.RLock.release" title="threading.RLock.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> as many times may lead to deadlock. Consider using <code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code> as a context manager rather than calling acquire/release directly.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>timeout</em> parameter is new.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.RLock.release"> <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.RLock.release" title="Link to this definition">¶</a></dt> <dd><p>Release a lock, decrementing the recursion level. If after the decrement it is zero, reset the lock to unlocked (not owned by any thread), and if any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed. If after the decrement the recursion level is still nonzero, the lock remains locked and owned by the calling thread.</p> <p>Only call this method when the calling thread owns the lock. A <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised if this method is called when the lock is not acquired.</p> <p>There is no return value.</p> </dd></dl> </dd></dl> </section> <section id="condition-objects"> <span id="id4"></span><h2>Condition Objects<a class="headerlink" href="#condition-objects" title="Link to this heading">¶</a></h2> <p>A condition variable is always associated with some kind of lock; this can be passed in or one will be created by default. Passing one in is useful when several condition variables must share the same lock. The lock is part of the condition object: you don’t have to track it separately.</p> <p>A condition variable obeys the <a class="reference internal" href="#with-locks"><span class="std std-ref">context management protocol</span></a>: using the <code class="docutils literal notranslate"><span class="pre">with</span></code> statement acquires the associated lock for the duration of the enclosed block. The <a class="reference internal" href="#threading.Condition.acquire" title="threading.Condition.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> and <a class="reference internal" href="#threading.Condition.release" title="threading.Condition.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> methods also call the corresponding methods of the associated lock.</p> <p>Other methods must be called with the associated lock held. The <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method releases the lock, and then blocks until another thread awakens it by calling <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> or <a class="reference internal" href="#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a>. Once awakened, <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> re-acquires the lock and returns. It is also possible to specify a timeout.</p> <p>The <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> method wakes up one of the threads waiting for the condition variable, if any are waiting. The <a class="reference internal" href="#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a> method wakes up all threads waiting for the condition variable.</p> <p>Note: the <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> and <a class="reference internal" href="#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a> methods don’t release the lock; this means that the thread or threads awakened will not return from their <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> call immediately, but only when the thread that called <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> or <a class="reference internal" href="#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a> finally relinquishes ownership of the lock.</p> <p>The typical programming style using condition variables uses the lock to synchronize access to some shared state; threads that are interested in a particular change of state call <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> repeatedly until they see the desired state, while threads that modify the state call <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> or <a class="reference internal" href="#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a> when they change the state in such a way that it could possibly be a desired state for one of the waiters. For example, the following code is a generic producer-consumer situation with unlimited buffer capacity:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Consume one item</span> <span class="k">with</span> <span class="n">cv</span><span class="p">:</span> <span class="k">while</span> <span class="ow">not</span> <span class="n">an_item_is_available</span><span class="p">():</span> <span class="n">cv</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="n">get_an_available_item</span><span class="p">()</span> <span class="c1"># Produce one item</span> <span class="k">with</span> <span class="n">cv</span><span class="p">:</span> <span class="n">make_an_item_available</span><span class="p">()</span> <span class="n">cv</span><span class="o">.</span><span class="n">notify</span><span class="p">()</span> </pre></div> </div> <p>The <code class="docutils literal notranslate"><span class="pre">while</span></code> loop checking for the application’s condition is necessary because <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> can return after an arbitrary long time, and the condition which prompted the <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> call may no longer hold true. This is inherent to multi-threaded programming. The <a class="reference internal" href="#threading.Condition.wait_for" title="threading.Condition.wait_for"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait_for()</span></code></a> method can be used to automate the condition checking, and eases the computation of timeouts:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Consume an item</span> <span class="k">with</span> <span class="n">cv</span><span class="p">:</span> <span class="n">cv</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">an_item_is_available</span><span class="p">)</span> <span class="n">get_an_available_item</span><span class="p">()</span> </pre></div> </div> <p>To choose between <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> and <a class="reference internal" href="#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a>, consider whether one state change can be interesting for only one or several waiting threads. E.g. in a typical producer-consumer situation, adding one item to the buffer only needs to wake up one consumer thread.</p> <dl class="py class"> <dt class="sig sig-object py" id="threading.Condition"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">Condition</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">lock</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="#threading.Condition" title="Link to this definition">¶</a></dt> <dd><p>This class implements condition variable objects. A condition variable allows one or more threads to wait until they are notified by another thread.</p> <p>If the <em>lock</em> argument is given and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it must be a <a class="reference internal" href="#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a> or <a class="reference internal" href="#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> object, and it is used as the underlying lock. Otherwise, a new <a class="reference internal" href="#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> object is created and used as the underlying lock.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>changed from a factory function to a class.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="threading.Condition.acquire"> <span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.Condition.acquire" title="Link to this definition">¶</a></dt> <dd><p>Acquire the underlying lock. This method calls the corresponding method on the underlying lock; the return value is whatever that method returns.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Condition.release"> <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Condition.release" title="Link to this definition">¶</a></dt> <dd><p>Release the underlying lock. This method calls the corresponding method on the underlying lock; there is no return value.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Condition.wait"> <span class="sig-name descname"><span class="pre">wait</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="#threading.Condition.wait" title="Link to this definition">¶</a></dt> <dd><p>Wait until notified or until a timeout occurs. If the calling thread has not acquired the lock when this method is called, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p> <p>This method releases the underlying lock, and then blocks until it is awakened by a <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> or <a class="reference internal" href="#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify_all()</span></code></a> call for the same condition variable in another thread, or until the optional timeout occurs. Once awakened or timed out, it re-acquires the lock and returns.</p> <p>When the <em>timeout</em> argument is present and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it should be a floating-point number specifying a timeout for the operation in seconds (or fractions thereof).</p> <p>When the underlying lock is an <a class="reference internal" href="#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a>, it is not released using its <a class="reference internal" href="#threading.Condition.release" title="threading.Condition.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> method, since this may not actually unlock the lock when it was acquired multiple times recursively. Instead, an internal interface of the <a class="reference internal" href="#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a> class is used, which really unlocks it even when it has been recursively acquired several times. Another internal interface is then used to restore the recursion level when the lock is reacquired.</p> <p>The return value is <code class="docutils literal notranslate"><span class="pre">True</span></code> unless a given <em>timeout</em> expired, in which case it is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Previously, the method always returned <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Condition.wait_for"> <span class="sig-name descname"><span class="pre">wait_for</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">predicate</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="#threading.Condition.wait_for" title="Link to this definition">¶</a></dt> <dd><p>Wait until a condition evaluates to true. <em>predicate</em> should be a callable which result will be interpreted as a boolean value. A <em>timeout</em> may be provided giving the maximum time to wait.</p> <p>This utility method may call <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> repeatedly until the predicate is satisfied, or until a timeout occurs. The return value is the last return value of the predicate and will evaluate to <code class="docutils literal notranslate"><span class="pre">False</span></code> if the method timed out.</p> <p>Ignoring the timeout feature, calling this method is roughly equivalent to writing:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="ow">not</span> <span class="n">predicate</span><span class="p">():</span> <span class="n">cv</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> </pre></div> </div> <p>Therefore, the same rules apply as with <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a>: The lock must be held when called and is re-acquired on return. The predicate is evaluated with the lock held.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.2.</span></p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Condition.notify"> <span class="sig-name descname"><span class="pre">notify</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.Condition.notify" title="Link to this definition">¶</a></dt> <dd><p>By default, wake up one thread waiting on this condition, if any. If the calling thread has not acquired the lock when this method is called, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p> <p>This method wakes up at most <em>n</em> of the threads waiting for the condition variable; it is a no-op if no threads are waiting.</p> <p>The current implementation wakes up exactly <em>n</em> threads, if at least <em>n</em> threads are waiting. However, it’s not safe to rely on this behavior. A future, optimized implementation may occasionally wake up more than <em>n</em> threads.</p> <p>Note: an awakened thread does not actually return from its <a class="reference internal" href="#threading.Condition.wait" title="threading.Condition.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> call until it can reacquire the lock. Since <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a> does not release the lock, its caller should.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Condition.notify_all"> <span class="sig-name descname"><span class="pre">notify_all</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Condition.notify_all" title="Link to this definition">¶</a></dt> <dd><p>Wake up all threads waiting on this condition. This method acts like <a class="reference internal" href="#threading.Condition.notify" title="threading.Condition.notify"><code class="xref py py-meth docutils literal notranslate"><span class="pre">notify()</span></code></a>, but wakes up all waiting threads instead of one. If the calling thread has not acquired the lock when this method is called, a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p> <p>The method <code class="docutils literal notranslate"><span class="pre">notifyAll</span></code> is a deprecated alias for this method.</p> </dd></dl> </dd></dl> </section> <section id="semaphore-objects"> <span id="id5"></span><h2>Semaphore Objects<a class="headerlink" href="#semaphore-objects" title="Link to this heading">¶</a></h2> <p>This is one of the oldest synchronization primitives in the history of computer science, invented by the early Dutch computer scientist Edsger W. Dijkstra (he used the names <code class="docutils literal notranslate"><span class="pre">P()</span></code> and <code class="docutils literal notranslate"><span class="pre">V()</span></code> instead of <a class="reference internal" href="#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> and <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a>).</p> <p>A semaphore manages an internal counter which is decremented by each <a class="reference internal" href="#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> call and incremented by each <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> call. The counter can never go below zero; when <a class="reference internal" href="#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> finds that it is zero, it blocks, waiting until some other thread calls <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a>.</p> <p>Semaphores also support the <a class="reference internal" href="#with-locks"><span class="std std-ref">context management protocol</span></a>.</p> <dl class="py class"> <dt class="sig sig-object py" id="threading.Semaphore"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">Semaphore</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.Semaphore" title="Link to this definition">¶</a></dt> <dd><p>This class implements semaphore objects. A semaphore manages an atomic counter representing the number of <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> calls minus the number of <a class="reference internal" href="#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> calls, plus an initial value. The <a class="reference internal" href="#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> method blocks if necessary until it can return without making the counter negative. If not given, <em>value</em> defaults to 1.</p> <p>The optional argument gives the initial <em>value</em> for the internal counter; it defaults to <code class="docutils literal notranslate"><span class="pre">1</span></code>. If the <em>value</em> given is less than 0, <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> is raised.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>changed from a factory function to a class.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="threading.Semaphore.acquire"> <span class="sig-name descname"><span class="pre">acquire</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">blocking</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="#threading.Semaphore.acquire" title="Link to this definition">¶</a></dt> <dd><p>Acquire a semaphore.</p> <p>When invoked without arguments:</p> <ul class="simple"> <li><p>If the internal counter is larger than zero on entry, decrement it by one and return <code class="docutils literal notranslate"><span class="pre">True</span></code> immediately.</p></li> <li><p>If the internal counter is zero on entry, block until awoken by a call to <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a>. Once awoken (and the counter is greater than 0), decrement the counter by 1 and return <code class="docutils literal notranslate"><span class="pre">True</span></code>. Exactly one thread will be awoken by each call to <a class="reference internal" href="#threading.Semaphore.release" title="threading.Semaphore.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a>. The order in which threads are awoken should not be relied on.</p></li> </ul> <p>When invoked with <em>blocking</em> set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, do not block. If a call without an argument would block, return <code class="docutils literal notranslate"><span class="pre">False</span></code> immediately; otherwise, do the same thing as when called without arguments, and return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p> <p>When invoked with a <em>timeout</em> other than <code class="docutils literal notranslate"><span class="pre">None</span></code>, it will block for at most <em>timeout</em> seconds. If acquire does not complete successfully in that interval, return <code class="docutils literal notranslate"><span class="pre">False</span></code>. Return <code class="docutils literal notranslate"><span class="pre">True</span></code> otherwise.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>timeout</em> parameter is new.</p> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Semaphore.release"> <span class="sig-name descname"><span class="pre">release</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.Semaphore.release" title="Link to this definition">¶</a></dt> <dd><p>Release a semaphore, incrementing the internal counter by <em>n</em>. When it was zero on entry and other threads are waiting for it to become larger than zero again, wake up <em>n</em> of those threads.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Added the <em>n</em> parameter to release multiple waiting threads at once.</p> </div> </dd></dl> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="threading.BoundedSemaphore"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">BoundedSemaphore</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#threading.BoundedSemaphore" title="Link to this definition">¶</a></dt> <dd><p>Class implementing bounded semaphore objects. A bounded semaphore checks to make sure its current value doesn’t exceed its initial value. If it does, <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> is raised. In most situations semaphores are used to guard resources with limited capacity. If the semaphore is released too many times it’s a sign of a bug. If not given, <em>value</em> defaults to 1.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>changed from a factory function to a class.</p> </div> </dd></dl> <section id="semaphore-example"> <span id="semaphore-examples"></span><h3><a class="reference internal" href="#threading.Semaphore" title="threading.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code></a> Example<a class="headerlink" href="#semaphore-example" title="Link to this heading">¶</a></h3> <p>Semaphores are often used to guard resources with limited capacity, for example, a database server. In any situation where the size of the resource is fixed, you should use a bounded semaphore. Before spawning any worker threads, your main thread would initialize the semaphore:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">maxconnections</span> <span class="o">=</span> <span class="mi">5</span> <span class="c1"># ...</span> <span class="n">pool_sema</span> <span class="o">=</span> <span class="n">BoundedSemaphore</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="n">maxconnections</span><span class="p">)</span> </pre></div> </div> <p>Once spawned, worker threads call the semaphore’s acquire and release methods when they need to connect to the server:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">pool_sema</span><span class="p">:</span> <span class="n">conn</span> <span class="o">=</span> <span class="n">connectdb</span><span class="p">()</span> <span class="k">try</span><span class="p">:</span> <span class="c1"># ... use connection ...</span> <span class="k">finally</span><span class="p">:</span> <span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> </pre></div> </div> <p>The use of a bounded semaphore reduces the chance that a programming error which causes the semaphore to be released more than it’s acquired will go undetected.</p> </section> </section> <section id="event-objects"> <span id="id6"></span><h2>Event Objects<a class="headerlink" href="#event-objects" title="Link to this heading">¶</a></h2> <p>This is one of the simplest mechanisms for communication between threads: one thread signals an event and other threads wait for it.</p> <p>An event object manages an internal flag that can be set to true with the <a class="reference internal" href="#threading.Event.set" title="threading.Event.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> method and reset to false with the <a class="reference internal" href="#threading.Event.clear" title="threading.Event.clear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clear()</span></code></a> method. The <a class="reference internal" href="#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method blocks until the flag is true.</p> <dl class="py class"> <dt class="sig sig-object py" id="threading.Event"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">Event</span></span><a class="headerlink" href="#threading.Event" title="Link to this definition">¶</a></dt> <dd><p>Class implementing event objects. An event manages a flag that can be set to true with the <a class="reference internal" href="#threading.Event.set" title="threading.Event.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> method and reset to false with the <a class="reference internal" href="#threading.Event.clear" title="threading.Event.clear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clear()</span></code></a> method. The <a class="reference internal" href="#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method blocks until the flag is true. The flag is initially false.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>changed from a factory function to a class.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="threading.Event.is_set"> <span class="sig-name descname"><span class="pre">is_set</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Event.is_set" title="Link to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if and only if the internal flag is true.</p> <p>The method <code class="docutils literal notranslate"><span class="pre">isSet</span></code> is a deprecated alias for this method.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Event.set"> <span class="sig-name descname"><span class="pre">set</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Event.set" title="Link to this definition">¶</a></dt> <dd><p>Set the internal flag to true. All threads waiting for it to become true are awakened. Threads that call <a class="reference internal" href="#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> once the flag is true will not block at all.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Event.clear"> <span class="sig-name descname"><span class="pre">clear</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Event.clear" title="Link to this definition">¶</a></dt> <dd><p>Reset the internal flag to false. Subsequently, threads calling <a class="reference internal" href="#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> will block until <a class="reference internal" href="#threading.Event.set" title="threading.Event.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> is called to set the internal flag to true again.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Event.wait"> <span class="sig-name descname"><span class="pre">wait</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="#threading.Event.wait" title="Link to this definition">¶</a></dt> <dd><p>Block as long as the internal flag is false and the timeout, if given, has not expired. The return value represents the reason that this blocking method returned; <code class="docutils literal notranslate"><span class="pre">True</span></code> if returning because the internal flag is set to true, or <code class="docutils literal notranslate"><span class="pre">False</span></code> if a timeout is given and the internal flag did not become true within the given wait time.</p> <p>When the timeout argument is present and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it should be a floating-point number specifying a timeout for the operation in seconds, or fractions thereof.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.1: </span>Previously, the method always returned <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </div> </dd></dl> </dd></dl> </section> <section id="timer-objects"> <span id="id7"></span><h2>Timer Objects<a class="headerlink" href="#timer-objects" title="Link to this heading">¶</a></h2> <p>This class represents an action that should be run only after a certain amount of time has passed — a timer. <a class="reference internal" href="#threading.Timer" title="threading.Timer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Timer</span></code></a> is a subclass of <a class="reference internal" href="#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">Thread</span></code></a> and as such also functions as an example of creating custom threads.</p> <p>Timers are started, as with threads, by calling their <a class="reference internal" href="#threading.Thread.start" title="threading.Thread.start"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Timer.start</span></code></a> method. The timer can be stopped (before its action has begun) by calling the <a class="reference internal" href="#threading.Timer.cancel" title="threading.Timer.cancel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">cancel()</span></code></a> method. The interval the timer will wait before executing its action may not be exactly the same as the interval specified by the user.</p> <p>For example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">hello</span><span class="p">():</span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;hello, world&quot;</span><span class="p">)</span> <span class="n">t</span> <span class="o">=</span> <span class="n">Timer</span><span class="p">(</span><span class="mf">30.0</span><span class="p">,</span> <span class="n">hello</span><span class="p">)</span> <span class="n">t</span><span class="o">.</span><span class="n">start</span><span class="p">()</span> <span class="c1"># after 30 seconds, &quot;hello, world&quot; will be printed</span> </pre></div> </div> <dl class="py class"> <dt class="sig sig-object py" id="threading.Timer"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">Timer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">interval</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">function</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</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="#threading.Timer" title="Link to this definition">¶</a></dt> <dd><p>Create a timer that will run <em>function</em> with arguments <em>args</em> and keyword arguments <em>kwargs</em>, after <em>interval</em> seconds have passed. If <em>args</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) then an empty list will be used. If <em>kwargs</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) then an empty dict will be used.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>changed from a factory function to a class.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="threading.Timer.cancel"> <span class="sig-name descname"><span class="pre">cancel</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Timer.cancel" title="Link to this definition">¶</a></dt> <dd><p>Stop the timer, and cancel the execution of the timer’s action. This will only work if the timer is still in its waiting stage.</p> </dd></dl> </dd></dl> </section> <section id="barrier-objects"> <h2>Barrier Objects<a class="headerlink" href="#barrier-objects" title="Link to this heading">¶</a></h2> <div class="versionadded"> <p><span class="versionmodified added">Added in version 3.2.</span></p> </div> <p>This class provides a simple synchronization primitive for use by a fixed number of threads that need to wait for each other. Each of the threads tries to pass the barrier by calling the <a class="reference internal" href="#threading.Barrier.wait" title="threading.Barrier.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method and will block until all of the threads have made their <a class="reference internal" href="#threading.Barrier.wait" title="threading.Barrier.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> calls. At this point, the threads are released simultaneously.</p> <p>The barrier can be reused any number of times for the same number of threads.</p> <p>As an example, here is a simple way to synchronize a client and server thread:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">b</span> <span class="o">=</span> <span class="n">Barrier</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span> <span class="k">def</span> <span class="nf">server</span><span class="p">():</span> <span class="n">start_server</span><span class="p">()</span> <span class="n">b</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="k">while</span> <span class="kc">True</span><span class="p">:</span> <span class="n">connection</span> <span class="o">=</span> <span class="n">accept_connection</span><span class="p">()</span> <span class="n">process_server_connection</span><span class="p">(</span><span class="n">connection</span><span class="p">)</span> <span class="k">def</span> <span class="nf">client</span><span class="p">():</span> <span class="n">b</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="k">while</span> <span class="kc">True</span><span class="p">:</span> <span class="n">connection</span> <span class="o">=</span> <span class="n">make_connection</span><span class="p">()</span> <span class="n">process_client_connection</span><span class="p">(</span><span class="n">connection</span><span class="p">)</span> </pre></div> </div> <dl class="py class"> <dt class="sig sig-object py" id="threading.Barrier"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">Barrier</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parties</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">action</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</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="#threading.Barrier" title="Link to this definition">¶</a></dt> <dd><p>Create a barrier object for <em>parties</em> number of threads. An <em>action</em>, when provided, is a callable to be called by one of the threads when they are released. <em>timeout</em> is the default timeout value if none is specified for the <a class="reference internal" href="#threading.Barrier.wait" title="threading.Barrier.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method.</p> <dl class="py method"> <dt class="sig sig-object py" id="threading.Barrier.wait"> <span class="sig-name descname"><span class="pre">wait</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="#threading.Barrier.wait" title="Link to this definition">¶</a></dt> <dd><p>Pass the barrier. When all the threads party to the barrier have called this function, they are all released simultaneously. If a <em>timeout</em> is provided, it is used in preference to any that was supplied to the class constructor.</p> <p>The return value is an integer in the range 0 to <em>parties</em> – 1, different for each thread. This can be used to select a thread to do some special housekeeping, e.g.:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="n">barrier</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="c1"># Only one thread needs to print this</span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;passed the barrier&quot;</span><span class="p">)</span> </pre></div> </div> <p>If an <em>action</em> was provided to the constructor, one of the threads will have called it prior to being released. Should this call raise an error, the barrier is put into the broken state.</p> <p>If the call times out, the barrier is put into the broken state.</p> <p>This method may raise a <a class="reference internal" href="#threading.BrokenBarrierError" title="threading.BrokenBarrierError"><code class="xref py py-class docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a> exception if the barrier is broken or reset while a thread is waiting.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Barrier.reset"> <span class="sig-name descname"><span class="pre">reset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Barrier.reset" title="Link to this definition">¶</a></dt> <dd><p>Return the barrier to the default, empty state. Any threads waiting on it will receive the <a class="reference internal" href="#threading.BrokenBarrierError" title="threading.BrokenBarrierError"><code class="xref py py-class docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a> exception.</p> <p>Note that using this function may require some external synchronization if there are other threads whose state is unknown. If a barrier is broken it may be better to just leave it and create a new one.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="threading.Barrier.abort"> <span class="sig-name descname"><span class="pre">abort</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#threading.Barrier.abort" title="Link to this definition">¶</a></dt> <dd><p>Put the barrier into a broken state. This causes any active or future calls to <a class="reference internal" href="#threading.Barrier.wait" title="threading.Barrier.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> to fail with the <a class="reference internal" href="#threading.BrokenBarrierError" title="threading.BrokenBarrierError"><code class="xref py py-class docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a>. Use this for example if one of the threads needs to abort, to avoid deadlocking the application.</p> <p>It may be preferable to simply create the barrier with a sensible <em>timeout</em> value to automatically guard against one of the threads going awry.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="threading.Barrier.parties"> <span class="sig-name descname"><span class="pre">parties</span></span><a class="headerlink" href="#threading.Barrier.parties" title="Link to this definition">¶</a></dt> <dd><p>The number of threads required to pass the barrier.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="threading.Barrier.n_waiting"> <span class="sig-name descname"><span class="pre">n_waiting</span></span><a class="headerlink" href="#threading.Barrier.n_waiting" title="Link to this definition">¶</a></dt> <dd><p>The number of threads currently waiting in the barrier.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="threading.Barrier.broken"> <span class="sig-name descname"><span class="pre">broken</span></span><a class="headerlink" href="#threading.Barrier.broken" title="Link to this definition">¶</a></dt> <dd><p>A boolean that is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the barrier is in the broken state.</p> </dd></dl> </dd></dl> <dl class="py exception"> <dt class="sig sig-object py" id="threading.BrokenBarrierError"> <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">threading.</span></span><span class="sig-name descname"><span class="pre">BrokenBarrierError</span></span><a class="headerlink" href="#threading.BrokenBarrierError" title="Link to this definition">¶</a></dt> <dd><p>This exception, a subclass of <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>, is raised when the <a class="reference internal" href="#threading.Barrier" title="threading.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a> object is reset or broken.</p> </dd></dl> </section> <section id="using-locks-conditions-and-semaphores-in-the-with-statement"> <span id="with-locks"></span><h2>Using locks, conditions, and semaphores in the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement<a class="headerlink" href="#using-locks-conditions-and-semaphores-in-the-with-statement" title="Link to this heading">¶</a></h2> <p>All of the objects provided by this module that have <code class="docutils literal notranslate"><span class="pre">acquire</span></code> and <code class="docutils literal notranslate"><span class="pre">release</span></code> methods can be used as context managers for a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement. The <code class="docutils literal notranslate"><span class="pre">acquire</span></code> method will be called when the block is entered, and <code class="docutils literal notranslate"><span class="pre">release</span></code> will be called when the block is exited. Hence, the following snippet:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">some_lock</span><span class="p">:</span> <span class="c1"># do something...</span> </pre></div> </div> <p>is equivalent to:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">some_lock</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span> <span class="k">try</span><span class="p">:</span> <span class="c1"># do something...</span> <span class="k">finally</span><span class="p">:</span> <span class="n">some_lock</span><span class="o">.</span><span class="n">release</span><span class="p">()</span> </pre></div> </div> <p>Currently, <a class="reference internal" href="#threading.Lock" title="threading.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lock</span></code></a>, <a class="reference internal" href="#threading.RLock" title="threading.RLock"><code class="xref py py-class docutils literal notranslate"><span class="pre">RLock</span></code></a>, <a class="reference internal" href="#threading.Condition" title="threading.Condition"><code class="xref py py-class docutils literal notranslate"><span class="pre">Condition</span></code></a>, <a class="reference internal" href="#threading.Semaphore" title="threading.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code></a>, and <a class="reference internal" href="#threading.BoundedSemaphore" title="threading.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundedSemaphore</span></code></a> objects may be used as <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement context managers.</p> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="Main"> <div class="sphinxsidebarwrapper"> <div> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> — Thread-based parallelism</a><ul> <li><a class="reference internal" href="#threading.active_count"><code class="docutils literal notranslate"><span class="pre">active_count()</span></code></a></li> <li><a class="reference internal" href="#threading.current_thread"><code class="docutils literal notranslate"><span class="pre">current_thread()</span></code></a></li> <li><a class="reference internal" href="#threading.excepthook"><code class="docutils literal notranslate"><span class="pre">excepthook()</span></code></a></li> <li><a class="reference internal" href="#threading.__excepthook__"><code class="docutils literal notranslate"><span class="pre">__excepthook__</span></code></a></li> <li><a class="reference internal" href="#threading.get_ident"><code class="docutils literal notranslate"><span class="pre">get_ident()</span></code></a></li> <li><a class="reference internal" href="#threading.get_native_id"><code class="docutils literal notranslate"><span class="pre">get_native_id()</span></code></a></li> <li><a class="reference internal" href="#threading.enumerate"><code class="docutils literal notranslate"><span class="pre">enumerate()</span></code></a></li> <li><a class="reference internal" href="#threading.main_thread"><code class="docutils literal notranslate"><span class="pre">main_thread()</span></code></a></li> <li><a class="reference internal" href="#threading.settrace"><code class="docutils literal notranslate"><span class="pre">settrace()</span></code></a></li> <li><a class="reference internal" href="#threading.settrace_all_threads"><code class="docutils literal notranslate"><span class="pre">settrace_all_threads()</span></code></a></li> <li><a class="reference internal" href="#threading.gettrace"><code class="docutils literal notranslate"><span class="pre">gettrace()</span></code></a></li> <li><a class="reference internal" href="#threading.setprofile"><code class="docutils literal notranslate"><span class="pre">setprofile()</span></code></a></li> <li><a class="reference internal" href="#threading.setprofile_all_threads"><code class="docutils literal notranslate"><span class="pre">setprofile_all_threads()</span></code></a></li> <li><a class="reference internal" href="#threading.getprofile"><code class="docutils literal notranslate"><span class="pre">getprofile()</span></code></a></li> <li><a class="reference internal" href="#threading.stack_size"><code class="docutils literal notranslate"><span class="pre">stack_size()</span></code></a></li> <li><a class="reference internal" href="#threading.TIMEOUT_MAX"><code class="docutils literal notranslate"><span class="pre">TIMEOUT_MAX</span></code></a></li> <li><a class="reference internal" href="#thread-local-data">Thread-Local Data</a><ul> <li><a class="reference internal" href="#threading.local"><code class="docutils literal notranslate"><span class="pre">local</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#thread-objects">Thread Objects</a><ul> <li><a class="reference internal" href="#threading.Thread"><code class="docutils literal notranslate"><span class="pre">Thread</span></code></a><ul> <li><a class="reference internal" href="#threading.Thread.start"><code class="docutils literal notranslate"><span class="pre">start()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.run"><code class="docutils literal notranslate"><span class="pre">run()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.join"><code class="docutils literal notranslate"><span class="pre">join()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.name"><code class="docutils literal notranslate"><span class="pre">name</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.getName"><code class="docutils literal notranslate"><span class="pre">getName()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.setName"><code class="docutils literal notranslate"><span class="pre">setName()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.ident"><code class="docutils literal notranslate"><span class="pre">ident</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.native_id"><code class="docutils literal notranslate"><span class="pre">native_id</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.is_alive"><code class="docutils literal notranslate"><span class="pre">is_alive()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.daemon"><code class="docutils literal notranslate"><span class="pre">daemon</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.isDaemon"><code class="docutils literal notranslate"><span class="pre">isDaemon()</span></code></a></li> <li><a class="reference internal" href="#threading.Thread.setDaemon"><code class="docutils literal notranslate"><span class="pre">setDaemon()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#lock-objects">Lock Objects</a><ul> <li><a class="reference internal" href="#threading.Lock"><code class="docutils literal notranslate"><span class="pre">Lock</span></code></a><ul> <li><a class="reference internal" href="#threading.Lock.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.Lock.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> <li><a class="reference internal" href="#threading.Lock.locked"><code class="docutils literal notranslate"><span class="pre">locked()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#rlock-objects">RLock Objects</a><ul> <li><a class="reference internal" href="#threading.RLock"><code class="docutils literal notranslate"><span class="pre">RLock</span></code></a><ul> <li><a class="reference internal" href="#threading.RLock.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.RLock.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#condition-objects">Condition Objects</a><ul> <li><a class="reference internal" href="#threading.Condition"><code class="docutils literal notranslate"><span class="pre">Condition</span></code></a><ul> <li><a class="reference internal" href="#threading.Condition.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.wait"><code class="docutils literal notranslate"><span class="pre">wait()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.wait_for"><code class="docutils literal notranslate"><span class="pre">wait_for()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.notify"><code class="docutils literal notranslate"><span class="pre">notify()</span></code></a></li> <li><a class="reference internal" href="#threading.Condition.notify_all"><code class="docutils literal notranslate"><span class="pre">notify_all()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#semaphore-objects">Semaphore Objects</a><ul> <li><a class="reference internal" href="#threading.Semaphore"><code class="docutils literal notranslate"><span class="pre">Semaphore</span></code></a><ul> <li><a class="reference internal" href="#threading.Semaphore.acquire"><code class="docutils literal notranslate"><span class="pre">acquire()</span></code></a></li> <li><a class="reference internal" href="#threading.Semaphore.release"><code class="docutils literal notranslate"><span class="pre">release()</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#threading.BoundedSemaphore"><code class="docutils literal notranslate"><span class="pre">BoundedSemaphore</span></code></a></li> <li><a class="reference internal" href="#semaphore-example"><code class="xref py py-class docutils literal notranslate"><span class="pre">Semaphore</span></code> Example</a></li> </ul> </li> <li><a class="reference internal" href="#event-objects">Event Objects</a><ul> <li><a class="reference internal" href="#threading.Event"><code class="docutils literal notranslate"><span class="pre">Event</span></code></a><ul> <li><a class="reference internal" href="#threading.Event.is_set"><code class="docutils literal notranslate"><span class="pre">is_set()</span></code></a></li> <li><a class="reference internal" href="#threading.Event.set"><code class="docutils literal notranslate"><span class="pre">set()</span></code></a></li> <li><a class="reference internal" href="#threading.Event.clear"><code class="docutils literal notranslate"><span class="pre">clear()</span></code></a></li> <li><a class="reference internal" href="#threading.Event.wait"><code class="docutils literal notranslate"><span class="pre">wait()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#timer-objects">Timer Objects</a><ul> <li><a class="reference internal" href="#threading.Timer"><code class="docutils literal notranslate"><span class="pre">Timer</span></code></a><ul> <li><a class="reference internal" href="#threading.Timer.cancel"><code class="docutils literal notranslate"><span class="pre">cancel()</span></code></a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#barrier-objects">Barrier Objects</a><ul> <li><a class="reference internal" href="#threading.Barrier"><code class="docutils literal notranslate"><span class="pre">Barrier</span></code></a><ul> <li><a class="reference internal" href="#threading.Barrier.wait"><code class="docutils literal notranslate"><span class="pre">wait()</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.reset"><code class="docutils literal notranslate"><span class="pre">reset()</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.abort"><code class="docutils literal notranslate"><span class="pre">abort()</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.parties"><code class="docutils literal notranslate"><span class="pre">parties</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.n_waiting"><code class="docutils literal notranslate"><span class="pre">n_waiting</span></code></a></li> <li><a class="reference internal" href="#threading.Barrier.broken"><code class="docutils literal notranslate"><span class="pre">broken</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#threading.BrokenBarrierError"><code class="docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#using-locks-conditions-and-semaphores-in-the-with-statement">Using locks, conditions, and semaphores in the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement</a></li> </ul> </li> </ul> </div> <div> <h4>Previous topic</h4> <p class="topless"><a href="concurrency.html" title="previous chapter">Concurrent Execution</a></p> </div> <div> <h4>Next topic</h4> <p class="topless"><a href="multiprocessing.html" title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code> — Process-based parallelism</a></p> </div> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/main/Doc/library/threading.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> <div id="sidebarbutton" title="Collapse sidebar"> <span>«</span> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="Related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="multiprocessing.html" title="multiprocessing — Process-based parallelism" >next</a> |</li> <li class="right" > <a href="concurrency.html" title="Concurrent Execution" >previous</a> |</li> <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> &#187;</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.13.1 Documentation</a> &#187; </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li> <li class="nav-item nav-item-2"><a href="concurrency.html" >Concurrent Execution</a> &#187;</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> — Thread-based parallelism</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" /> <input type="submit" value="Go" /> </form> </div> | </li> <li class="right"> <label class="theme-selector-label"> Theme <select class="theme-selector" oninput="activateTheme(this.value)"> <option value="auto" selected>Auto</option> <option value="light">Light</option> <option value="dark">Dark</option> </select> </label> |</li> </ul> </div> <div class="footer"> &copy; <a href="../copyright.html"> Copyright </a> 2001-2024, Python Software Foundation. <br /> This page is licensed under the Python Software Foundation License Version 2. <br /> Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. <br /> See <a href="/license.html">History and License</a> for more information.<br /> <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> <br /> Last updated on Dec 04, 2024 (20:46 UTC). <a href="/bugs.html">Found a bug</a>? <br /> Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3. </div> <script type="text/javascript" src="../_static/switchers.js"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10