CINXE.COM

gevent.monkey – Make the standard library cooperative - gevent 24.11.2.dev0 documentation

<!doctype html> <html class="no-js" lang="en" data-content_root="../"> <head><meta charset="utf-8"/> <meta name="viewport" content="width=device-width,initial-scale=1"/> <meta name="color-scheme" content="light dark"><meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="index" title="Index" href="../genindex.html" /><link rel="search" title="Search" href="../search.html" /><link rel="next" title="gevent.os – Low-level operating system functions from os" href="gevent.os.html" /><link rel="prev" title="gevent.fileobject – Wrappers to make file-like objects cooperative" href="gevent.fileobject.html" /> <!-- Generated with Sphinx 8.1.3 and Furo 2024.08.06 --> <title>gevent.monkey – Make the standard library cooperative - gevent 24.11.2.dev0 documentation</title> <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a3258c64" /> <link rel="stylesheet" type="text/css" href="../_static/styles/furo.css?v=354aac6f" /> <link rel="stylesheet" type="text/css" href="../_static/styles/furo-extensions.css?v=302659d7" /> <link rel="stylesheet" type="text/css" href="../_static/custom.css?v=e405f49d" /> <style> body { --color-code-background: #eeeedd; --color-code-foreground: black; --color-brand-primary: #7c9a5e; --color-brand-content: #7c9a5e; --color-foreground-border: #b7d897; --font-stack: "SF Pro",-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"; --font-stack--monospace: "JetBrainsMono", "JetBrains Mono", "JetBrains Mono Regular", "JetBrainsMono-Regular", ui-monospace, profont, monospace; } @media not print { body[data-theme="dark"] { --color-code-background: #202020; --color-code-foreground: #d0d0d0; } @media (prefers-color-scheme: dark) { body:not([data-theme="light"]) { --color-code-background: #202020; --color-code-foreground: #d0d0d0; } } } </style><script async type="text/javascript" src="/_/static/javascript/readthedocs-addons.js"></script><meta name="readthedocs-project-slug" content="python-gevent" /><meta name="readthedocs-version-slug" content="latest" /><meta name="readthedocs-resolver-filename" content="/api/gevent.monkey.html" /><meta name="readthedocs-http-status" content="200" /></head> <body> <script> document.body.dataset.theme = localStorage.getItem("theme") || "auto"; </script> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="svg-toc" viewBox="0 0 24 24"> <title>Contents</title> <svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024"> <path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/> </svg> </symbol> <symbol id="svg-menu" viewBox="0 0 24 24"> <title>Menu</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu"> <line x1="3" y1="12" x2="21" y2="12"></line> <line x1="3" y1="6" x2="21" y2="6"></line> <line x1="3" y1="18" x2="21" y2="18"></line> </svg> </symbol> <symbol id="svg-arrow-right" viewBox="0 0 24 24"> <title>Expand</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right"> <polyline points="9 18 15 12 9 6"></polyline> </svg> </symbol> <symbol id="svg-sun" viewBox="0 0 24 24"> <title>Light mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="feather-sun"> <circle cx="12" cy="12" r="5"></circle> <line x1="12" y1="1" x2="12" y2="3"></line> <line x1="12" y1="21" x2="12" y2="23"></line> <line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line> <line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line> <line x1="1" y1="12" x2="3" y2="12"></line> <line x1="21" y1="12" x2="23" y2="12"></line> <line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line> <line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line> </svg> </symbol> <symbol id="svg-moon" viewBox="0 0 24 24"> <title>Dark mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" /> </svg> </symbol> <symbol id="svg-sun-with-moon" viewBox="0 0 24 24"> <title>Auto light/dark, in light mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-custom-derived-from-feather-sun-and-tabler-moon"> <path style="opacity: 50%" d="M 5.411 14.504 C 5.471 14.504 5.532 14.504 5.591 14.504 C 3.639 16.319 4.383 19.569 6.931 20.352 C 7.693 20.586 8.512 20.551 9.25 20.252 C 8.023 23.207 4.056 23.725 2.11 21.184 C 0.166 18.642 1.702 14.949 4.874 14.536 C 5.051 14.512 5.231 14.5 5.411 14.5 L 5.411 14.504 Z"/> <line x1="14.5" y1="3.25" x2="14.5" y2="1.25"/> <line x1="14.5" y1="15.85" x2="14.5" y2="17.85"/> <line x1="10.044" y1="5.094" x2="8.63" y2="3.68"/> <line x1="19" y1="14.05" x2="20.414" y2="15.464"/> <line x1="8.2" y1="9.55" x2="6.2" y2="9.55"/> <line x1="20.8" y1="9.55" x2="22.8" y2="9.55"/> <line x1="10.044" y1="14.006" x2="8.63" y2="15.42"/> <line x1="19" y1="5.05" x2="20.414" y2="3.636"/> <circle cx="14.5" cy="9.55" r="3.6"/> </svg> </symbol> <symbol id="svg-moon-with-sun" viewBox="0 0 24 24"> <title>Auto light/dark, in dark mode</title> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-custom-derived-from-feather-sun-and-tabler-moon"> <path d="M 8.282 7.007 C 8.385 7.007 8.494 7.007 8.595 7.007 C 5.18 10.184 6.481 15.869 10.942 17.24 C 12.275 17.648 13.706 17.589 15 17.066 C 12.851 22.236 5.91 23.143 2.505 18.696 C -0.897 14.249 1.791 7.786 7.342 7.063 C 7.652 7.021 7.965 7 8.282 7 L 8.282 7.007 Z"/> <line style="opacity: 50%" x1="18" y1="3.705" x2="18" y2="2.5"/> <line style="opacity: 50%" x1="18" y1="11.295" x2="18" y2="12.5"/> <line style="opacity: 50%" x1="15.316" y1="4.816" x2="14.464" y2="3.964"/> <line style="opacity: 50%" x1="20.711" y1="10.212" x2="21.563" y2="11.063"/> <line style="opacity: 50%" x1="14.205" y1="7.5" x2="13.001" y2="7.5"/> <line style="opacity: 50%" x1="21.795" y1="7.5" x2="23" y2="7.5"/> <line style="opacity: 50%" x1="15.316" y1="10.184" x2="14.464" y2="11.036"/> <line style="opacity: 50%" x1="20.711" y1="4.789" x2="21.563" y2="3.937"/> <circle style="opacity: 50%" cx="18" cy="7.5" r="2.169"/> </svg> </symbol> <symbol id="svg-pencil" viewBox="0 0 24 24"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-pencil-code"> <path d="M4 20h4l10.5 -10.5a2.828 2.828 0 1 0 -4 -4l-10.5 10.5v4" /> <path d="M13.5 6.5l4 4" /> <path d="M20 21l2 -2l-2 -2" /> <path d="M17 17l-2 2l2 2" /> </svg> </symbol> <symbol id="svg-eye" viewBox="0 0 24 24"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-eye-code"> <path stroke="none" d="M0 0h24v24H0z" fill="none" /> <path d="M10 12a2 2 0 1 0 4 0a2 2 0 0 0 -4 0" /> <path d="M11.11 17.958c-3.209 -.307 -5.91 -2.293 -8.11 -5.958c2.4 -4 5.4 -6 9 -6c3.6 0 6.6 2 9 6c-.21 .352 -.427 .688 -.647 1.008" /> <path d="M20 21l2 -2l-2 -2" /> <path d="M17 17l-2 2l2 2" /> </svg> </symbol> </svg> <input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation"> <input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc"> <label class="overlay sidebar-overlay" for="__navigation"> <div class="visually-hidden">Hide navigation sidebar</div> </label> <label class="overlay toc-overlay" for="__toc"> <div class="visually-hidden">Hide table of contents sidebar</div> </label> <a class="skip-to-content muted-link" href="#furo-main-content">Skip to content</a> <div class="page"> <header class="mobile-header"> <div class="header-left"> <label class="nav-overlay-icon" for="__navigation"> <div class="visually-hidden">Toggle site navigation sidebar</div> <i class="icon"><svg><use href="#svg-menu"></use></svg></i> </label> </div> <div class="header-center"> <a href="../contents.html"><div class="brand">gevent 24.11.2.dev0 documentation</div></a> </div> <div class="header-right"> <div class="theme-toggle-container theme-toggle-header"> <button class="theme-toggle"> <div class="visually-hidden">Toggle Light / Dark / Auto color theme</div> <svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg> <svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg> <svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg> <svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg> </button> </div> <label class="toc-overlay-icon toc-header-icon" for="__toc"> <div class="visually-hidden">Toggle table of contents sidebar</div> <i class="icon"><svg><use href="#svg-toc"></use></svg></i> </label> </div> </header> <aside class="sidebar-drawer"> <div class="sidebar-container"> <div class="sidebar-sticky"><a class="sidebar-brand" href="../contents.html"> <div class="sidebar-logo-container"> <img class="sidebar-logo" src="../_static/5564530.png" alt="Logo"/> </div> </a><form class="sidebar-search-container" method="get" action="../search.html" role="search"> <input class="sidebar-search" placeholder="Search" name="q" aria-label="Search"> <input type="hidden" name="check_keywords" value="yes"> <input type="hidden" name="area" value="default"> </form> <div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree"> <ul> <li class="toctree-l1"><a class="reference internal" href="../install.html">Installation and Requirements</a></li> <li class="toctree-l1 has-children"><a class="reference internal" href="../changelog.html">Changelog</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle navigation of Changelog</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_5.html">Changes for 1.5</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_4.html">Changes for 1.4</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_3.html">Changes for 1.3</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_2.html">Changes for 1.2</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_1.html">Changes for 1.1</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_0.html">Changes for 1.0</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_pre.html">Changes before gevent 1.0</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../intro.html">Introduction</a></li> <li class="toctree-l1"><a class="reference internal" href="gevent.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent</span></code> – common functions</a></li> <li class="toctree-l1"><a class="reference internal" href="gevent.greenlet.html">Greenlet Objects</a></li> <li class="toctree-l1"><a class="reference internal" href="../servers.html">Implementing servers</a></li> <li class="toctree-l1 has-children"><a class="reference internal" href="../dns.html">Name Resolution (DNS)</a><input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" role="switch" type="checkbox"/><label for="toctree-checkbox-2"><div class="visually-hidden">Toggle navigation of Name Resolution (DNS)</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.thread.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.thread</span></code> – thread based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.ares.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.ares</span></code> – c-ares based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.dnspython.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.dnspython</span></code> – Pure Python hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.blocking.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.blocking</span></code> – Non-cooperative resolver</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../monitoring.html">Monitoring and Debugging gevent Applications</a></li> <li class="toctree-l1"><a class="reference internal" href="../loop_impls.html">Event Loop Implementations: libuv and libev</a></li> <li class="toctree-l1"><a class="reference internal" href="../configuration.html">Configuring gevent</a></li> </ul> <ul class="current"> <li class="toctree-l1 current has-children"><a class="reference internal" href="index.html">API reference</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" role="switch" type="checkbox"/><label for="toctree-checkbox-3"><div class="visually-hidden">Toggle navigation of API reference</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current"> <li class="toctree-l2"><a class="reference internal" href="gevent.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent</span></code> – common functions</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.timeout.html">Cooperative Timeouts Using <code class="docutils literal notranslate"><span class="pre">gevent.Timeout</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.greenlet.html">Greenlet Objects</a></li> <li class="toctree-l2 has-children"><a class="reference internal" href="gevent.socket.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.socket</span></code> – Cooperative low-level networking interface</a><input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" role="switch" type="checkbox"/><label for="toctree-checkbox-4"><div class="visually-hidden">Toggle navigation of gevent.socket – Cooperative low-level networking interface</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="gevent._socket3.html">Python 3 interface</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="gevent.ssl.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.ssl</span></code> – Secure Sockets Layer (SSL/TLS) module</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.select.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.select</span></code> – Waiting for I/O completion</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.selectors.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.selectors</span></code> – High-level IO Multiplexing</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.event.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.event</span></code> – Notifications of multiple listeners</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.queue.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.queue</span></code> – Synchronized queues</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.local.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.local</span></code> – Greenlet-local objects</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.lock.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.lock</span></code> – Locking primitives</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.hub.html"><code class="docutils literal notranslate"><span class="pre">gevent.hub</span></code> - The Event Loop and the Hub</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.core.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.core</span></code> - (deprecated) event loop abstraction</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent</span></code> – common functions</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.backdoor.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.backdoor</span></code> – Interactive greenlet-based network console that can be used in any process</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.baseserver.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.baseserver</span></code> – Base class for implementing servers</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.builtins.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.builtins</span></code> – gevent friendly implementations of builtin functions</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.contextvars.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.contextvars</span></code> – Cooperative <code class="docutils literal notranslate"><span class="pre">contextvars</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.core.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.core</span></code> - (deprecated) event loop abstraction</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.event.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.event</span></code> – Notifications of multiple listeners</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.events.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.events</span></code> – Publish/subscribe event infrastructure</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.exceptions.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.exceptions</span></code> – Exceptions</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.fileobject.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.fileobject</span></code> – Wrappers to make file-like objects cooperative</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.hub.html"><code class="docutils literal notranslate"><span class="pre">gevent.hub</span></code> - The Event Loop and the Hub</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.local.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.local</span></code> – Greenlet-local objects</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.lock.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.lock</span></code> – Locking primitives</a></li> <li class="toctree-l2 current current-page"><a class="current reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.monkey</span></code> – Make the standard library cooperative</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.os.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.os</span></code> – Low-level operating system functions from <code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.pool.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.pool</span></code> – Managing greenlets in a group</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.pywsgi.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.pywsgi</span></code> – A pure-Python, gevent-friendly WSGI server</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.queue.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.queue</span></code> – Synchronized queues</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.ares.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.ares</span></code> – c-ares based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.blocking.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.blocking</span></code> – Non-cooperative resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.dnspython.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.dnspython</span></code> – Pure Python hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.resolver.thread.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.thread</span></code> – thread based hostname resolver</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.select.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.select</span></code> – Waiting for I/O completion</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.selectors.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.selectors</span></code> – High-level IO Multiplexing</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.server.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.server</span></code> – TCP/SSL server</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.signal.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.signal</span></code> – Cooperative implementation of special cases of <code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a></li> <li class="toctree-l2 has-children"><a class="reference internal" href="gevent.socket.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.socket</span></code> – Cooperative low-level networking interface</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" role="switch" type="checkbox"/><label for="toctree-checkbox-5"><div class="visually-hidden">Toggle navigation of gevent.socket – Cooperative low-level networking interface</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="gevent._socket3.html">Python 3 interface</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="gevent.ssl.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.ssl</span></code> – Secure Sockets Layer (SSL/TLS) module</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.subprocess.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.subprocess</span></code> – Cooperative <code class="docutils literal notranslate"><span class="pre">subprocess</span></code> module</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.thread.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.thread</span></code> – Implementation of the standard <code class="xref py py-mod docutils literal notranslate"><span class="pre">thread</span></code> module that spawns greenlets</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.threading.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.threading</span></code> – Implementation of the standard <code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code> using greenlets</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.threadpool.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.threadpool</span></code> - A pool of native threads</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.time.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.time</span></code> – Makes <em>sleep</em> gevent aware</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.util.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.util</span></code> – Low-level utilities</a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.ares.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.ares</span></code> – Backwards compatibility alias for <code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.resolver.cares</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="gevent.wsgi.html"><code class="docutils literal notranslate"><span class="pre">gevent.wsgi</span></code> – Historical note only; does not exist</a></li> <li class="toctree-l2 has-children"><a class="reference internal" href="../examples/index.html">Examples</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" role="switch" type="checkbox"/><label for="toctree-checkbox-6"><div class="visually-hidden">Toggle navigation of Examples</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../examples/concurrent_download.html">Example concurrent_download.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/dns_mass_resolve.html">Example dns_mass_resolve.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/echoserver.html">Example echoserver.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/geventsendfile.html">Example geventsendfile.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/portforwarder.html">Example portforwarder.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/processes.html">Example processes.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/psycopg2_pool.html">Example psycopg2_pool.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/threadpool.html">Example threadpool.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/udp_client.html">Example udp_client.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/udp_server.html">Example udp_server.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/unixsocket_client.html">Example unixsocket_client.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/unixsocket_server.html">Example unixsocket_server.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/webproxy.html">Example webproxy.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/webpy.html">Example webpy.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/wsgiserver.html">Example wsgiserver.py</a></li> <li class="toctree-l3"><a class="reference internal" href="../examples/wsgiserver_ssl.html">Example wsgiserver_ssl.py</a></li> </ul> </li> </ul> </li> </ul> <ul> <li class="toctree-l1 has-children"><a class="reference internal" href="../development/index.html">Development</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" role="switch" type="checkbox"/><label for="toctree-checkbox-7"><div class="visually-hidden">Toggle navigation of Development</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2"><a class="reference internal" href="../development/getting_started.html">Getting Started</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/installing_from_source.html">Installing From Source</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/running_tests.html">Running Tests</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/ci.html">Continuous integration</a></li> <li class="toctree-l2"><a class="reference internal" href="../development/release_process.html">Release Process</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../success.html">Success stories</a></li> <li class="toctree-l1"><a class="reference internal" href="../community.html">Community</a></li> <li class="toctree-l1 has-children"><a class="reference internal" href="../older_releases.html">Information About Older Releases</a><input class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" role="switch" type="checkbox"/><label for="toctree-checkbox-8"><div class="visually-hidden">Toggle navigation of Information About Older Releases</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_5.html">What’s new in gevent 1.5</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" role="switch" type="checkbox"/><label for="toctree-checkbox-9"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.5</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_5.html">Changes for 1.5</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_4.html">What’s new in gevent 1.4</a><input class="toctree-checkbox" id="toctree-checkbox-10" name="toctree-checkbox-10" role="switch" type="checkbox"/><label for="toctree-checkbox-10"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.4</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_4.html">Changes for 1.4</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_3.html">What’s new in gevent 1.3</a><input class="toctree-checkbox" id="toctree-checkbox-11" name="toctree-checkbox-11" role="switch" type="checkbox"/><label for="toctree-checkbox-11"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.3</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_3.html">Changes for 1.3</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_2.html">What’s new in gevent 1.2</a><input class="toctree-checkbox" id="toctree-checkbox-12" name="toctree-checkbox-12" role="switch" type="checkbox"/><label for="toctree-checkbox-12"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.2</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_2.html">Changes for 1.2</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_1.html">What’s new in gevent 1.1</a><input class="toctree-checkbox" id="toctree-checkbox-13" name="toctree-checkbox-13" role="switch" type="checkbox"/><label for="toctree-checkbox-13"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.1</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_1.html">Changes for 1.1</a></li> </ul> </li> <li class="toctree-l2 has-children"><a class="reference internal" href="../whatsnew_1_0.html">What’s new in gevent 1.0</a><input class="toctree-checkbox" id="toctree-checkbox-14" name="toctree-checkbox-14" role="switch" type="checkbox"/><label for="toctree-checkbox-14"><div class="visually-hidden">Toggle navigation of What’s new in gevent 1.0</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul> <li class="toctree-l3"><a class="reference internal" href="../changelog_1_0.html">Changes for 1.0</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_5.html">Changes for 1.5</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_4.html">Changes for 1.4</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_3.html">Changes for 1.3</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_2.html">Changes for 1.2</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_1.html">Changes for 1.1</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_1_0.html">Changes for 1.0</a></li> <li class="toctree-l2"><a class="reference internal" href="../changelog_pre.html">Changes before gevent 1.0</a></li> </ul> </li> </ul> </div> </div> </div> </div> </aside> <div class="main"> <div class="content"> <div class="article-container"> <a href="#" class="back-to-top muted-link"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> <path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12z"></path> </svg> <span>Back to top</span> </a> <div class="content-icon-container"> <div class="theme-toggle-container theme-toggle-content"> <button class="theme-toggle"> <div class="visually-hidden">Toggle Light / Dark / Auto color theme</div> <svg class="theme-icon-when-auto-light"><use href="#svg-sun-with-moon"></use></svg> <svg class="theme-icon-when-auto-dark"><use href="#svg-moon-with-sun"></use></svg> <svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg> <svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg> </button> </div> <label class="toc-overlay-icon toc-content-icon" for="__toc"> <div class="visually-hidden">Toggle table of contents sidebar</div> <i class="icon"><svg><use href="#svg-toc"></use></svg></i> </label> </div> <article role="main" id="furo-main-content"> <section id="module-gevent.monkey"> <span id="gevent-monkey-make-the-standard-library-cooperative"></span><h1><a class="reference internal" href="#module-gevent.monkey" title="gevent.monkey"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.monkey</span></code></a> – Make the standard library cooperative<a class="headerlink" href="#module-gevent.monkey" title="Link to this heading">¶</a></h1> <p>Make the standard library cooperative.</p> <p>The primary purpose of this module is to carefully patch, in place, portions of the standard library with gevent-friendly functions that behave in the same way as the original (at least as closely as possible).</p> <p>The primary interface to this is the <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a> function, which performs all the available patches. It accepts arguments to limit the patching to certain modules, but most programs <strong>should</strong> use the default values as they receive the most wide-spread testing, and some monkey patches have dependencies on others.</p> <p>Patching <strong>should be done as early as possible</strong> in the lifecycle of the program. For example, the main module (the one that tests against <code class="docutils literal notranslate"><span class="pre">__main__</span></code> or is otherwise the first imported) should begin with this code, ideally before any other imports:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">gevent</span> <span class="kn">import</span> <span class="n">monkey</span> <span class="n">monkey</span><span class="o">.</span><span class="n">patch_all</span><span class="p">()</span> </pre></div> </div> <p>A corollary of the above is that patching <strong>should be done on the main thread</strong> and <strong>should be done while the program is single-threaded</strong>.</p> <div class="admonition tip"> <p class="admonition-title">Tip</p> <p>Some frameworks, such as gunicorn, handle monkey-patching for you. Check their documentation to be sure.</p> </div> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>Patching too late can lead to unreliable behaviour (for example, some modules may still use blocking sockets) or even errors.</p> </div> <div class="admonition tip"> <p class="admonition-title">Tip</p> <p>Be sure to read the documentation for each patch function to check for known incompatibilities.</p> </div> <section id="querying"> <h2>Querying<a class="headerlink" href="#querying" title="Link to this heading">¶</a></h2> <p>Sometimes it is helpful to know if objects have been monkey-patched, and in advanced cases even to have access to the original standard library functions. This module provides functions for that purpose.</p> <ul class="simple"> <li><p><a class="reference internal" href="#gevent.monkey.is_module_patched" title="gevent.monkey.is_module_patched"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_module_patched()</span></code></a></p></li> <li><p><a class="reference internal" href="#gevent.monkey.is_object_patched" title="gevent.monkey.is_object_patched"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_object_patched()</span></code></a></p></li> <li><p><a class="reference internal" href="#gevent.monkey.get_original" title="gevent.monkey.get_original"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_original()</span></code></a></p></li> </ul> </section> <section id="plugins-and-events"> <span id="plugins"></span><h2>Plugins and Events<a class="headerlink" href="#plugins-and-events" title="Link to this heading">¶</a></h2> <p>Beginning in gevent 1.3, events are emitted during the monkey patching process. These events are delivered first to <a class="reference internal" href="gevent.events.html#module-gevent.events" title="gevent.events"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.events</span></code></a> subscribers, and then to <a class="reference external" href="http://setuptools.readthedocs.io/en/latest/setuptools.html#dynamic-discovery-of-services-and-plugins">setuptools entry points</a>.</p> <p>The following events are defined. They are listed in (roughly) the order that a call to <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a> will emit them.</p> <ul class="simple"> <li><p><a class="reference internal" href="gevent.events.html#gevent.events.GeventWillPatchAllEvent" title="gevent.events.GeventWillPatchAllEvent"><code class="xref py py-class docutils literal notranslate"><span class="pre">gevent.events.GeventWillPatchAllEvent</span></code></a></p></li> <li><p><a class="reference internal" href="gevent.events.html#gevent.events.GeventWillPatchModuleEvent" title="gevent.events.GeventWillPatchModuleEvent"><code class="xref py py-class docutils literal notranslate"><span class="pre">gevent.events.GeventWillPatchModuleEvent</span></code></a></p></li> <li><p><a class="reference internal" href="gevent.events.html#gevent.events.GeventDidPatchModuleEvent" title="gevent.events.GeventDidPatchModuleEvent"><code class="xref py py-class docutils literal notranslate"><span class="pre">gevent.events.GeventDidPatchModuleEvent</span></code></a></p></li> <li><p><a class="reference internal" href="gevent.events.html#gevent.events.GeventDidPatchBuiltinModulesEvent" title="gevent.events.GeventDidPatchBuiltinModulesEvent"><code class="xref py py-class docutils literal notranslate"><span class="pre">gevent.events.GeventDidPatchBuiltinModulesEvent</span></code></a></p></li> <li><p><a class="reference internal" href="gevent.events.html#gevent.events.GeventDidPatchAllEvent" title="gevent.events.GeventDidPatchAllEvent"><code class="xref py py-class docutils literal notranslate"><span class="pre">gevent.events.GeventDidPatchAllEvent</span></code></a></p></li> </ul> <p>Each event class documents the corresponding setuptools entry point name. The entry points will be called with a single argument, the same instance of the class that was sent to the subscribers.</p> <p>You can subscribe to the events to monitor the monkey-patching process and to manipulate it, for example by raising <a class="reference internal" href="gevent.events.html#gevent.events.DoNotPatch" title="gevent.events.DoNotPatch"><code class="xref py py-exc docutils literal notranslate"><span class="pre">gevent.events.DoNotPatch</span></code></a>.</p> <p>You can also subscribe to the events to provide additional patching beyond what gevent distributes, either for additional standard library modules, or for third-party packages. The suggested time to do this patching is in the subscriber for <a class="reference internal" href="gevent.events.html#gevent.events.GeventDidPatchBuiltinModulesEvent" title="gevent.events.GeventDidPatchBuiltinModulesEvent"><code class="xref py py-class docutils literal notranslate"><span class="pre">gevent.events.GeventDidPatchBuiltinModulesEvent</span></code></a>. For example, to automatically patch <a class="reference external" href="https://pypi.python.org/pypi/psycopg2">psycopg2</a> using <a class="reference external" href="https://pypi.python.org/pypi/psycogreen">psycogreen</a> when the call to <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a> is made, you could write code like this:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># mypackage.py</span> <span class="k">def</span> <span class="nf">patch_psycopg</span><span class="p">(</span><span class="n">event</span><span class="p">):</span> <span class="kn">from</span> <span class="nn">psycogreen.gevent</span> <span class="kn">import</span> <span class="n">patch_psycopg</span> <span class="n">patch_psycopg</span><span class="p">()</span> </pre></div> </div> <p>In your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> you would register it like this:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">setuptools</span> <span class="kn">import</span> <span class="n">setup</span> <span class="n">setup</span><span class="p">(</span> <span class="o">...</span> <span class="n">entry_points</span><span class="o">=</span><span class="p">{</span> <span class="s1">&#39;gevent.plugins.monkey.did_patch_builtins&#39;</span><span class="p">:</span> <span class="p">[</span> <span class="s1">&#39;psycopg2 = mypackage:patch_psycopg&#39;</span><span class="p">,</span> <span class="p">],</span> <span class="p">},</span> <span class="o">...</span> <span class="p">)</span> </pre></div> </div> <p>For more complex patching, gevent provides a helper method that you can call to replace attributes of modules with attributes of your own modules. This function also takes care of emitting the appropriate events.</p> <ul class="simple"> <li><p><a class="reference internal" href="#gevent.monkey.patch_module" title="gevent.monkey.patch_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_module()</span></code></a></p></li> </ul> </section> <section id="use-as-a-module"> <h2>Use as a module<a class="headerlink" href="#use-as-a-module" title="Link to this heading">¶</a></h2> <p>Sometimes it is useful to run existing python scripts or modules that were not built to be gevent aware under gevent. To do so, this module can be run as the main module, passing the script and its arguments. For details, see the <code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code> function.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.3b1: </span>Added support for plugins and began emitting will/did patch events.</p> </div> </section> <dl class="py exception"> <dt class="sig sig-object py" id="gevent.monkey.MonkeyPatchWarning"> <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">MonkeyPatchWarning</span></span><a class="reference internal" href="../_modules/gevent/monkey/_errors.html#MonkeyPatchWarning"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.MonkeyPatchWarning" title="Link to this definition">¶</a></dt> <dd><p>Bases: <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#RuntimeWarning" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">RuntimeWarning</span></code></a></p> <p>The type of warnings we issue.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 1.3a2.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.get_original"> <span class="sig-name descname"><span class="pre">get_original</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mod_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">item_name</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey/api.html#get_original"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.get_original" title="Link to this definition">¶</a></dt> <dd><p>Retrieve the original object from a module.</p> <p>If the object has not been patched, then that object will still be retrieved.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters<span class="colon">:</span></dt> <dd class="field-odd"><ul class="simple"> <li><p><strong>mod_name</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><em>str</em></a><em>|</em><em>sequence</em>) – The name of the standard library module, e.g., <code class="docutils literal notranslate"><span class="pre">'socket'</span></code>. Can also be a sequence of standard library modules giving alternate names to try, e.g., <code class="docutils literal notranslate"><span class="pre">('thread',</span> <span class="pre">'_thread')</span></code>; the first importable module will supply all <em>item_name</em> items.</p></li> <li><p><strong>item_name</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><em>str</em></a><em>|</em><em>sequence</em>) – A string or sequence of strings naming the attribute(s) on the module <code class="docutils literal notranslate"><span class="pre">mod_name</span></code> to return.</p></li> </ul> </dd> <dt class="field-even">Returns<span class="colon">:</span></dt> <dd class="field-even"><p>The original value if a string was given for <code class="docutils literal notranslate"><span class="pre">item_name</span></code> or a sequence of original values if a sequence was passed.</p> </dd> </dl> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.is_module_patched"> <span class="sig-name descname"><span class="pre">is_module_patched</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mod_name</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey/_state.html#is_module_patched"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.is_module_patched" title="Link to this definition">¶</a></dt> <dd><p>Check if a module has been replaced with a cooperative version.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters<span class="colon">:</span></dt> <dd class="field-odd"><p><strong>mod_name</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><em>str</em></a>) – The name of the standard library module, e.g., <code class="docutils literal notranslate"><span class="pre">'socket'</span></code>.</p> </dd> </dl> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.is_object_patched"> <span class="sig-name descname"><span class="pre">is_object_patched</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mod_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">item_name</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey/_state.html#is_object_patched"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.is_object_patched" title="Link to this definition">¶</a></dt> <dd><p>Check if an object in a module has been replaced with a cooperative version.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters<span class="colon">:</span></dt> <dd class="field-odd"><ul class="simple"> <li><p><strong>mod_name</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><em>str</em></a>) – The name of the standard library module, e.g., <code class="docutils literal notranslate"><span class="pre">'socket'</span></code>.</p></li> <li><p><strong>item_name</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><em>str</em></a>) – The name of the attribute in the module, e.g., <code class="docutils literal notranslate"><span class="pre">'create_connection'</span></code>.</p></li> </ul> </dd> </dl> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_all"> <span class="sig-name descname"><span class="pre">patch_all</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">socket</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">dns</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">time</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">select</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">thread</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">os</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">ssl</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">subprocess</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">sys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">aggressive</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">Event</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">builtins</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">signal</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">queue</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">contextvars</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="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_all"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_all" title="Link to this definition">¶</a></dt> <dd><p>Do all of the default monkey patching (calls every other applicable function in this module).</p> <dl class="field-list simple"> <dt class="field-odd">Returns<span class="colon">:</span></dt> <dd class="field-odd"><p>A true value if patching all modules wasn’t cancelled, a false value if it was.</p> </dd> </dl> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.1: </span>Issue a <a class="reference external" href="https://docs.python.org/3/library/warnings.html#module-warnings" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">warning</span></code></a> if this function is called multiple times with different arguments. The second and subsequent calls will only add more patches, they can never remove existing patches by setting an argument to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.1: </span>Issue a <a class="reference external" href="https://docs.python.org/3/library/warnings.html#module-warnings" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">warning</span></code></a> if this function is called with <code class="docutils literal notranslate"><span class="pre">os=False</span></code> and <code class="docutils literal notranslate"><span class="pre">signal=True</span></code>. This will cause SIGCHLD handlers to not be called. This may be an error in the future.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.3a2: </span><code class="docutils literal notranslate"><span class="pre">Event</span></code> defaults to True.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.3b1: </span>Defined the return values.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.3b1: </span>Add <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> for the benefit of event subscribers. CAUTION: gevent may add and interpret additional arguments in the future, so it is suggested to use prefixes for kwarg values to be interpreted by plugins, for example, <code class="xref py py-obj docutils literal notranslate"><span class="pre">patch_all(mylib_futures=True)</span></code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.3.5: </span>Add <em>queue</em>, defaulting to True, for Python 3.7.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.5: </span>Remove the <code class="docutils literal notranslate"><span class="pre">httplib</span></code> argument. Previously, setting it raised a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.5a3: </span>Add the <code class="docutils literal notranslate"><span class="pre">contextvars</span></code> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.5: </span>Better handling of patching more than once.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_builtins"> <span class="sig-name descname"><span class="pre">patch_builtins</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_builtins"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_builtins" title="Link to this definition">¶</a></dt> <dd><p>Make the builtin <a class="reference external" href="https://docs.python.org/3/library/functions.html#import__" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function <a class="reference external" href="https://github.com/gevent/gevent/issues/108">greenlet safe</a> under Python 2.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This does nothing under Python 3 as it is not necessary. Python 3 features improved import locks that are per-module, not global.</p> </div> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 23.7.0: </span>Does nothing on any supported platform.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_dns"> <span class="sig-name descname"><span class="pre">patch_dns</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_dns"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_dns" title="Link to this definition">¶</a></dt> <dd><p>Replace <a class="reference internal" href="../dns.html"><span class="doc">DNS functions</span></a> in <a class="reference external" href="https://docs.python.org/3/library/socket.html#module-socket" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> with cooperative versions.</p> <p>This is only useful if <a class="reference internal" href="#gevent.monkey.patch_socket" title="gevent.monkey.patch_socket"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_socket()</span></code></a> has been called and is done automatically by that method if requested.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_module"> <span class="sig-name descname"><span class="pre">patch_module</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">target_module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">source_module</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">items</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="reference internal" href="../_modules/gevent/monkey/api.html#patch_module"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_module" title="Link to this definition">¶</a></dt> <dd><p>Replace attributes in <em>target_module</em> with the attributes of the same name in <em>source_module</em>.</p> <p>The <em>source_module</em> can provide some attributes to customize the process:</p> <ul class="simple"> <li><p><code class="docutils literal notranslate"><span class="pre">__implements__</span></code> is a list of attribute names to copy; if not present, the <em>items</em> keyword argument is mandatory. <code class="docutils literal notranslate"><span class="pre">__implements__</span></code> must only have names from the standard library module in it.</p></li> <li><p><code class="docutils literal notranslate"><span class="pre">_gevent_will_monkey_patch(target_module,</span> <span class="pre">items,</span> <span class="pre">warn,</span> <span class="pre">**kwargs)</span></code></p></li> <li><p><code class="docutils literal notranslate"><span class="pre">_gevent_did_monkey_patch(target_module,</span> <span class="pre">items,</span> <span class="pre">warn,</span> <span class="pre">**kwargs)</span></code> These two functions in the <em>source_module</em> are called <em>if</em> they exist, before and after copying attributes, respectively. The “will” function may modify <em>items</em>. The value of <em>warn</em> is a function that should be called with a single string argument to issue a warning to the user. If the “will” function raises <a class="reference internal" href="gevent.events.html#gevent.events.DoNotPatch" title="gevent.events.DoNotPatch"><code class="xref py py-exc docutils literal notranslate"><span class="pre">gevent.events.DoNotPatch</span></code></a>, no patching will be done. These functions are called before any event subscribers or plugins.</p></li> </ul> <dl class="field-list simple"> <dt class="field-odd">Parameters<span class="colon">:</span></dt> <dd class="field-odd"><p><strong>items</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#list" title="(in Python v3.13)"><em>list</em></a>) – A list of attribute names to replace. If not given, this will be taken from the <em>source_module</em> <code class="docutils literal notranslate"><span class="pre">__implements__</span></code> attribute.</p> </dd> <dt class="field-even">Returns<span class="colon">:</span></dt> <dd class="field-even"><p>A true value if patching was done, a false value if patching was canceled.</p> </dd> </dl> <div class="versionadded"> <p><span class="versionmodified added">Added in version 1.3b1.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_os"> <span class="sig-name descname"><span class="pre">patch_os</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_os"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_os" title="Link to this definition">¶</a></dt> <dd><p>Replace <a class="reference external" href="https://docs.python.org/3/library/os.html#os.fork" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> with <a class="reference internal" href="gevent.html#gevent.fork" title="gevent.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">gevent.fork()</span></code></a>, and, on POSIX, <a class="reference external" href="https://docs.python.org/3/library/os.html#os.waitpid" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.waitpid()</span></code></a> with <a class="reference internal" href="gevent.os.html#gevent.os.waitpid" title="gevent.os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">gevent.os.waitpid()</span></code></a> (if the environment variable <code class="docutils literal notranslate"><span class="pre">GEVENT_NOWAITPID</span></code> is not defined). Does nothing if fork is not available.</p> <div class="admonition caution"> <p class="admonition-title">Caution</p> <p>This method must be used with <a class="reference internal" href="#gevent.monkey.patch_signal" title="gevent.monkey.patch_signal"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_signal()</span></code></a> to have proper <code class="xref py py-obj docutils literal notranslate"><span class="pre">SIGCHLD</span></code> handling and thus correct results from <code class="docutils literal notranslate"><span class="pre">waitpid</span></code>. <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a> calls both by default.</p> </div> <div class="admonition caution"> <p class="admonition-title">Caution</p> <p>For <code class="xref py py-obj docutils literal notranslate"><span class="pre">SIGCHLD</span></code> handling to work correctly, the event loop must run. The easiest way to help ensure this is to use <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a>.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_queue"> <span class="sig-name descname"><span class="pre">patch_queue</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_queue"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_queue" title="Link to this definition">¶</a></dt> <dd><p>Patch objects in <a class="reference external" href="https://docs.python.org/3/library/queue.html#module-queue" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">queue</span></code></a>.</p> <p>Currently, this just replaces <a class="reference external" href="https://docs.python.org/3/library/queue.html#queue.SimpleQueue" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">queue.SimpleQueue</span></code></a> (implemented in C) with its Python counterpart, but the details may change at any time.</p> <div class="versionadded"> <p><span class="versionmodified added">Added in version 1.3.5.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_select"> <span class="sig-name descname"><span class="pre">patch_select</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">aggressive</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_select"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_select" title="Link to this definition">¶</a></dt> <dd><p>Replace <a class="reference external" href="https://docs.python.org/3/library/select.html#select.select" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.select()</span></code></a> with <a class="reference internal" href="gevent.select.html#gevent.select.select" title="gevent.select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">gevent.select.select()</span></code></a> and <a class="reference external" href="https://docs.python.org/3/library/select.html#select.poll" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.poll()</span></code></a> with <a class="reference internal" href="gevent.select.html#gevent.select.poll" title="gevent.select.poll"><code class="xref py py-class docutils literal notranslate"><span class="pre">gevent.select.poll</span></code></a> (where available).</p> <p>If <code class="docutils literal notranslate"><span class="pre">aggressive</span></code> is true (the default), also remove other blocking functions from <a class="reference external" href="https://docs.python.org/3/library/select.html#module-select" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a> .</p> <ul class="simple"> <li><p><a class="reference external" href="https://docs.python.org/3/library/select.html#select.epoll" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.epoll()</span></code></a></p></li> <li><p><a class="reference external" href="https://docs.python.org/3/library/select.html#select.kqueue" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.kqueue()</span></code></a></p></li> <li><p><a class="reference external" href="https://docs.python.org/3/library/select.html#select.kevent" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.kevent()</span></code></a></p></li> <li><p><a class="reference external" href="https://docs.python.org/3/library/select.html#select.devpoll" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.devpoll()</span></code></a> (Python 3.5+)</p></li> </ul> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_signal"> <span class="sig-name descname"><span class="pre">patch_signal</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_signal"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_signal" title="Link to this definition">¶</a></dt> <dd><p>Make the <a class="reference external" href="https://docs.python.org/3/library/signal.html#signal.signal" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a> function work with a <a class="reference internal" href="#gevent.monkey.patch_os" title="gevent.monkey.patch_os"><code class="xref py py-func docutils literal notranslate"><span class="pre">monkey-patched</span> <span class="pre">os</span></code></a>.</p> <div class="admonition caution"> <p class="admonition-title">Caution</p> <p>This method must be used with <a class="reference internal" href="#gevent.monkey.patch_os" title="gevent.monkey.patch_os"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_os()</span></code></a> to have proper <code class="docutils literal notranslate"><span class="pre">SIGCHLD</span></code> handling. <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a> calls both by default.</p> </div> <div class="admonition caution"> <p class="admonition-title">Caution</p> <p>For proper <code class="docutils literal notranslate"><span class="pre">SIGCHLD</span></code> handling, you must yield to the event loop. Using <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a> is the easiest way to ensure this.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="gevent.signal.html#module-gevent.signal" title="gevent.signal"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.signal</span></code></a></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_socket"> <span class="sig-name descname"><span class="pre">patch_socket</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dns</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">aggressive</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_socket"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_socket" title="Link to this definition">¶</a></dt> <dd><p>Replace the standard socket object with gevent’s cooperative sockets.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters<span class="colon">:</span></dt> <dd class="field-odd"><p><strong>dns</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – When true (the default), also patch address resolution functions in <a class="reference external" href="https://docs.python.org/3/library/socket.html#module-socket" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a>. See <a class="reference internal" href="../dns.html"><span class="doc">Name Resolution (DNS)</span></a> for details.</p> </dd> </dl> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_ssl"> <span class="sig-name descname"><span class="pre">patch_ssl</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/constants.html#None" title="(in Python v3.13)"><span class="pre">None</span></a></span></span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_ssl"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_ssl" title="Link to this definition">¶</a></dt> <dd><p>Replace <a class="reference external" href="https://docs.python.org/3/library/ssl.html#ssl.SSLSocket" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLSocket</span></code></a> object and socket wrapping functions in <a class="reference external" href="https://docs.python.org/3/library/ssl.html#module-ssl" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> with cooperative versions.</p> <p>This is only useful if <a class="reference internal" href="#gevent.monkey.patch_socket" title="gevent.monkey.patch_socket"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_socket()</span></code></a> has been called.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_subprocess"> <span class="sig-name descname"><span class="pre">patch_subprocess</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_subprocess"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_subprocess" title="Link to this definition">¶</a></dt> <dd><p>Replace <a class="reference external" href="https://docs.python.org/3/library/subprocess.html#subprocess.call" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.call()</span></code></a>, <a class="reference external" href="https://docs.python.org/3/library/subprocess.html#subprocess.check_call" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.check_call()</span></code></a>, <a class="reference external" href="https://docs.python.org/3/library/subprocess.html#subprocess.check_output" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.check_output()</span></code></a> and <a class="reference external" href="https://docs.python.org/3/library/subprocess.html#subprocess.Popen" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a> with <a class="reference internal" href="gevent.subprocess.html#module-gevent.subprocess" title="gevent.subprocess"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cooperative</span> <span class="pre">versions</span></code></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On Windows under Python 3, the API support may not completely match the standard library.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_sys"> <span class="sig-name descname"><span class="pre">patch_sys</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stdin</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">stdout</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">stderr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_sys"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_sys" title="Link to this definition">¶</a></dt> <dd><p>Patch sys.std[in,out,err] to use a cooperative IO via a threadpool.</p> <p>This is relatively dangerous and can have unintended consequences such as hanging the process or <a class="reference external" href="https://github.com/gevent/gevent/issues/274">misinterpreting control keys</a> when <a class="reference external" href="https://docs.python.org/3/library/functions.html#input" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">input()</span></code></a> and <code class="xref py py-func docutils literal notranslate"><span class="pre">raw_input()</span></code> are used. <a class="reference internal" href="#gevent.monkey.patch_all" title="gevent.monkey.patch_all"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch_all()</span></code></a> does <em>not</em> call this function by default.</p> <p>This method does nothing on Python 3. The Python 3 interpreter wants to flush the TextIOWrapper objects that make up stderr/stdout at shutdown time, but using a threadpool at that time leads to a hang.</p> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 23.7.0: </span>Does nothing on any supported version.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_thread"> <span class="sig-name descname"><span class="pre">patch_thread</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">threading</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">_threading_local</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">Event</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">logging</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">existing_locks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/constants.html#None" title="(in Python v3.13)"><span class="pre">None</span></a></span></span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_thread"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_thread" title="Link to this definition">¶</a></dt> <dd><p>Replace the standard <code class="xref py py-mod docutils literal notranslate"><span class="pre">thread</span></code> module to make it greenlet-based.</p> <dl class="field-list simple"> <dt class="field-odd">Parameters<span class="colon">:</span></dt> <dd class="field-odd"><ul class="simple"> <li><p><strong>threading</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – When True (the default), also patch <a class="reference external" href="https://docs.python.org/3/library/threading.html#module-threading" title="(in Python v3.13)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a>.</p></li> <li><p><strong>_threading_local</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – When True (the default), also patch <code class="xref py py-class docutils literal notranslate"><span class="pre">_threading_local.local</span></code>.</p></li> <li><p><strong>logging</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – When True (the default), also patch locks taken if the logging module has been configured.</p></li> <li><p><strong>existing_locks</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#bool" title="(in Python v3.13)"><em>bool</em></a>) – When True (the default), and the process is still single threaded, make sure that any <a class="reference external" href="https://docs.python.org/3/library/threading.html#threading.RLock" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.RLock</span></code></a> (and, under Python 3, <code class="xref py py-class docutils literal notranslate"><span class="pre">importlib._bootstrap._ModuleLock</span></code>) instances that are currently locked can be properly unlocked. <strong>Important</strong>: This is a best-effort attempt and, on certain implementations, may not detect all locks. It is important to monkey-patch extremely early in the startup process. Setting this to False is not recommended, especially on Python 2.</p></li> </ul> </dd> </dl> <div class="admonition caution"> <p class="admonition-title">Caution</p> <p>Monkey-patching <code class="xref py py-mod docutils literal notranslate"><span class="pre">thread</span></code> and using <a class="reference external" href="https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Queue" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.Queue</span></code></a> or <a class="reference external" href="https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="(in Python v3.13)"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ProcessPoolExecutor</span></code></a> (which uses a <code class="docutils literal notranslate"><span class="pre">Queue</span></code>) will hang the process.</p> <p>Monkey-patching with this function and using sub-interpreters (and advanced C-level API) and threads may be unstable on certain platforms.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.1b1: </span>Add <em>logging</em> and <em>existing_locks</em> params.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 1.3a2: </span><code class="docutils literal notranslate"><span class="pre">Event</span></code> defaults to True.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="gevent.monkey.patch_time"> <span class="sig-name descname"><span class="pre">patch_time</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="../_modules/gevent/monkey.html#patch_time"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#gevent.monkey.patch_time" title="Link to this definition">¶</a></dt> <dd><p>Replace <a class="reference external" href="https://docs.python.org/3/library/time.html#time.sleep" title="(in Python v3.13)"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.sleep()</span></code></a> with <a class="reference internal" href="gevent.html#gevent.sleep" title="gevent.sleep"><code class="xref py py-func docutils literal notranslate"><span class="pre">gevent.sleep()</span></code></a>.</p> </dd></dl> </section> </article> </div> <footer> <div class="related-pages"> <a class="next-page" href="gevent.os.html"> <div class="page-info"> <div class="context"> <span>Next</span> </div> <div class="title"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.os</span></code> – Low-level operating system functions from <code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></div> </div> <svg class="furo-related-icon"><use href="#svg-arrow-right"></use></svg> </a> <a class="prev-page" href="gevent.fileobject.html"> <svg class="furo-related-icon"><use href="#svg-arrow-right"></use></svg> <div class="page-info"> <div class="context"> <span>Previous</span> </div> <div class="title"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.fileobject</span></code> – Wrappers to make file-like objects cooperative</div> </div> </a> </div> <div class="bottom-of-page"> <div class="left-details"> <div class="copyright"> Copyright &#169; 2009-2023, gevent contributors </div> Made with <a href="https://www.sphinx-doc.org/">Sphinx</a> and <a class="muted-link" href="https://pradyunsg.me">@pradyunsg</a>'s <a href="https://github.com/pradyunsg/furo">Furo</a> </div> <div class="right-details"> </div> </div> </footer> </div> <aside class="toc-drawer"> <div class="toc-sticky toc-scroll"> <div class="toc-title-container"> <span class="toc-title"> On this page </span> </div> <div class="toc-tree-container"> <div class="toc-tree"> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gevent.monkey</span></code> – Make the standard library cooperative</a><ul> <li><a class="reference internal" href="#querying">Querying</a></li> <li><a class="reference internal" href="#plugins-and-events">Plugins and Events</a></li> <li><a class="reference internal" href="#use-as-a-module">Use as a module</a></li> <li><a class="reference internal" href="#gevent.monkey.MonkeyPatchWarning"><code class="docutils literal notranslate"><span class="pre">MonkeyPatchWarning</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.get_original"><code class="docutils literal notranslate"><span class="pre">get_original()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.is_module_patched"><code class="docutils literal notranslate"><span class="pre">is_module_patched()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.is_object_patched"><code class="docutils literal notranslate"><span class="pre">is_object_patched()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_all"><code class="docutils literal notranslate"><span class="pre">patch_all()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_builtins"><code class="docutils literal notranslate"><span class="pre">patch_builtins()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_dns"><code class="docutils literal notranslate"><span class="pre">patch_dns()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_module"><code class="docutils literal notranslate"><span class="pre">patch_module()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_os"><code class="docutils literal notranslate"><span class="pre">patch_os()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_queue"><code class="docutils literal notranslate"><span class="pre">patch_queue()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_select"><code class="docutils literal notranslate"><span class="pre">patch_select()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_signal"><code class="docutils literal notranslate"><span class="pre">patch_signal()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_socket"><code class="docutils literal notranslate"><span class="pre">patch_socket()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_ssl"><code class="docutils literal notranslate"><span class="pre">patch_ssl()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_subprocess"><code class="docutils literal notranslate"><span class="pre">patch_subprocess()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_sys"><code class="docutils literal notranslate"><span class="pre">patch_sys()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_thread"><code class="docutils literal notranslate"><span class="pre">patch_thread()</span></code></a></li> <li><a class="reference internal" href="#gevent.monkey.patch_time"><code class="docutils literal notranslate"><span class="pre">patch_time()</span></code></a></li> </ul> </li> </ul> </div> </div> </div> </aside> </div> </div><script src="../_static/documentation_options.js?v=32fdf4fc"></script> <script src="../_static/doctools.js?v=9bcbadda"></script> <script src="../_static/sphinx_highlight.js?v=dc90522c"></script> <script src="../_static/scripts/furo.js?v=5fa4622c"></script> </body> </html>

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