/usr/share/doc/python-gevent-doc/html/gevent.html is in python-gevent-doc 0.13.6-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>gevent – basic utilities — gevent v0.13.6 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.13.6',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="gevent v0.13.6 documentation" href="index.html" />
<link rel="up" title="API reference" href="reference.html" />
<link rel="next" title="Networking interfaces" href="networking.html" />
<link rel="prev" title="API reference" href="reference.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="networking.html" title="Networking interfaces"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="reference.html" title="API reference"
accesskey="P">previous</a> |</li>
<li><a href="http://www.gevent.org/">Home</a> »</li>
<li><a href="contents.html">Documentation</a> »</li>
<li><a href="reference.html" accesskey="U">API reference</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-gevent">
<span id="gevent-basic-utilities"></span><h1><a class="reference internal" href="#module-gevent" title="gevent"><tt class="xref py py-mod docutils literal"><span class="pre">gevent</span></tt></a> – basic utilities<a class="headerlink" href="#module-gevent" title="Permalink to this headline">¶</a></h1>
<p>The most common functions and classes are available in the <a class="reference internal" href="#module-gevent" title="gevent"><tt class="xref py py-mod docutils literal"><span class="pre">gevent</span></tt></a> top level package.</p>
<div class="section" id="greenlet-objects">
<h2>Greenlet objects<a class="headerlink" href="#greenlet-objects" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#gevent.Greenlet" title="gevent.Greenlet"><tt class="xref py py-class docutils literal"><span class="pre">Greenlet</span></tt></a> is a light-weight cooperatively-scheduled execution unit.</p>
<p>To start a new greenlet, pass the target function and its arguments to <a class="reference internal" href="#gevent.Greenlet" title="gevent.Greenlet"><tt class="xref py py-class docutils literal"><span class="pre">Greenlet</span></tt></a> constructor and call <tt class="xref py py-meth docutils literal"><span class="pre">start()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">g</span> <span class="o">=</span> <span class="n">Greenlet</span><span class="p">(</span><span class="n">myfunction</span><span class="p">,</span> <span class="s">'arg1'</span><span class="p">,</span> <span class="s">'arg2'</span><span class="p">,</span> <span class="n">kwarg1</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">g</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<p>or use classmethod <a class="reference internal" href="#gevent.spawn" title="gevent.spawn"><tt class="xref py py-meth docutils literal"><span class="pre">spawn()</span></tt></a> which is a shortcut that does the same:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">g</span> <span class="o">=</span> <span class="n">Greenlet</span><span class="o">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">myfunction</span><span class="p">,</span> <span class="s">'arg1'</span><span class="p">,</span> <span class="s">'arg2'</span><span class="p">,</span> <span class="n">kwarg1</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<p>To subclass a <a class="reference internal" href="#gevent.Greenlet" title="gevent.Greenlet"><tt class="xref py py-class docutils literal"><span class="pre">Greenlet</span></tt></a>, override its _run() method and call <tt class="docutils literal"><span class="pre">Greenlet.__init__(self)</span></tt> in <tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt>:
It also a good idea to override <tt class="xref py py-meth docutils literal"><span class="pre">__str__()</span></tt>: if <tt class="xref py py-meth docutils literal"><span class="pre">_run()</span></tt> raises an exception, its string representation will be printed after the traceback it generated.</p>
<dl class="class">
<dt id="gevent.Greenlet">
<em class="property">class </em><tt class="descclassname">gevent.</tt><tt class="descname">Greenlet</tt><a class="headerlink" href="#gevent.Greenlet" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="attribute">
<dt id="gevent.Greenlet.value">
<tt class="descclassname">Greenlet.</tt><tt class="descname">value</tt><a class="headerlink" href="#gevent.Greenlet.value" title="Permalink to this definition">¶</a></dt>
<dd><p>Holds the value returned by the function if the greenlet has finished successfully. Otherwise <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>
<dl class="attribute">
<dt id="gevent.Greenlet.exception">
<tt class="descclassname">Greenlet.</tt><tt class="descname">exception</tt><a class="headerlink" href="#gevent.Greenlet.exception" title="Permalink to this definition">¶</a></dt>
<dd><p>Holds the exception instance raised by the function if the greenlet has finished with an error.
Otherwise <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.ready">
<tt class="descclassname">Greenlet.</tt><tt class="descname">ready</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.Greenlet.ready" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if and only if the greenlet has finished execution.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.successful">
<tt class="descclassname">Greenlet.</tt><tt class="descname">successful</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.Greenlet.successful" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if and only if the greenlet has finished execution successfully,
that is, without raising an error.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.start">
<tt class="descclassname">Greenlet.</tt><tt class="descname">start</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.Greenlet.start" title="Permalink to this definition">¶</a></dt>
<dd><p>Schedule the greenlet to run in this loop iteration</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.start_later">
<tt class="descclassname">Greenlet.</tt><tt class="descname">start_later</tt><big>(</big><em>seconds</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.start_later" title="Permalink to this definition">¶</a></dt>
<dd><p>Schedule the greenlet to run in the future loop iteration <em>seconds</em> later</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.join">
<tt class="descclassname">Greenlet.</tt><tt class="descname">join</tt><big>(</big><em>timeout=None</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.join" title="Permalink to this definition">¶</a></dt>
<dd><p>Wait until the greenlet finishes or <em>timeout</em> expires.
Return <tt class="xref docutils literal"><span class="pre">None</span></tt> regardless.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.get">
<tt class="descclassname">Greenlet.</tt><tt class="descname">get</tt><big>(</big><em>block=True</em>, <em>timeout=None</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the result the greenlet has returned or re-raise the exception it has raised.</p>
<p>If block is <tt class="xref docutils literal"><span class="pre">False</span></tt>, raise <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">gevent.Timeout</span></tt></a> if the greenlet is still alive.
If block is <tt class="xref docutils literal"><span class="pre">True</span></tt>, unschedule the current greenlet until the result is available
or the timeout expires. In the latter case, <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">gevent.Timeout</span></tt></a> is raised.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.kill">
<tt class="descclassname">Greenlet.</tt><tt class="descname">kill</tt><big>(</big><em>exception=GreenletExit</em>, <em>block=False</em>, <em>timeout=None</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.kill" title="Permalink to this definition">¶</a></dt>
<dd><p>Raise the exception in the greenlet.</p>
<p>If block is <tt class="xref docutils literal"><span class="pre">True</span></tt> (the default), wait until the greenlet dies or the optional timeout expires.
If block is <tt class="xref docutils literal"><span class="pre">False</span></tt>, the current greenlet is not unscheduled.</p>
<p>The function always returns <tt class="xref docutils literal"><span class="pre">None</span></tt> and never raises an error.</p>
<p><cite>Changed in version 0.13.0:</cite> <em>block</em> is now <tt class="xref docutils literal"><span class="pre">True</span></tt> by default.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.link">
<tt class="descclassname">Greenlet.</tt><tt class="descname">link</tt><big>(</big><em>receiver=None</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.link" title="Permalink to this definition">¶</a></dt>
<dd><p>Link greenlet’s completion to callable or another greenlet.</p>
<p>If <em>receiver</em> is a callable then it will be called with this instance as an argument
once this greenlet’s dead. A callable is called in its own greenlet.</p>
<p>If <em>receiver</em> is a greenlet then an <tt class="xref py py-class docutils literal"><span class="pre">LinkedExited</span></tt> exception will be
raised in it once this greenlet’s dead.</p>
<p>If <em>receiver</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, link to the current greenlet.</p>
<p>Always asynchronous, unless receiver is a current greenlet and the result is ready.
If this greenlet is already dead, then notification will performed in this loop
iteration as soon as this greenlet switches to the hub.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.link_value">
<tt class="descclassname">Greenlet.</tt><tt class="descname">link_value</tt><big>(</big><em>receiver=None</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.link_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#gevent.Greenlet.link" title="gevent.Greenlet.link"><tt class="xref py py-meth docutils literal"><span class="pre">link()</span></tt></a> but <em>receiver</em> is only notified when the greenlet has completed successfully</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.link_exception">
<tt class="descclassname">Greenlet.</tt><tt class="descname">link_exception</tt><big>(</big><em>receiver=None</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.link_exception" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#gevent.Greenlet.link" title="gevent.Greenlet.link"><tt class="xref py py-meth docutils literal"><span class="pre">link()</span></tt></a> but <em>receiver</em> is only notified when the greenlet dies because of unhandled exception</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Greenlet.unlink">
<tt class="descclassname">Greenlet.</tt><tt class="descname">unlink</tt><big>(</big><em>receiver=None</em><big>)</big><a class="headerlink" href="#gevent.Greenlet.unlink" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove the receiver set by <a class="reference internal" href="#gevent.Greenlet.link" title="gevent.Greenlet.link"><tt class="xref py py-meth docutils literal"><span class="pre">link()</span></tt></a> or <tt class="xref py py-meth docutils literal"><span class="pre">rawlink()</span></tt></p>
</dd></dl>
<p>Being a <a class="reference external" href="http://codespeak.net/py/0.9.2/greenlet.html">greenlet</a> subclass, <a class="reference internal" href="#gevent.Greenlet" title="gevent.Greenlet"><tt class="xref py py-class docutils literal"><span class="pre">Greenlet</span></tt></a> also has <tt class="docutils literal"><span class="pre">switch()</span></tt> and <tt class="docutils literal"><span class="pre">throw()</span></tt> methods.
However, these should not be used at the application level. Prefer higher-level safe
classes, like <a class="reference internal" href="gevent.event.html#gevent.event.Event" title="gevent.event.Event"><tt class="xref py py-class docutils literal"><span class="pre">Event</span></tt></a> and <a class="reference internal" href="gevent.queue.html#gevent.queue.Queue" title="gevent.queue.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a>, instead.</p>
<dl class="exception">
<dt id="gevent.GreenletExit">
<em class="property">exception </em><tt class="descclassname">gevent.</tt><tt class="descname">GreenletExit</tt><a class="headerlink" href="#gevent.GreenletExit" title="Permalink to this definition">¶</a></dt>
<dd><p>A special exception that kills the greenlet silently.</p>
<p>When a greenlet raises <a class="reference internal" href="#gevent.GreenletExit" title="gevent.GreenletExit"><tt class="xref py py-exc docutils literal"><span class="pre">GreenletExit</span></tt></a> or a subclass, the traceback is not
printed and the greenlet is considered <a class="reference internal" href="#gevent.Greenlet.successful" title="gevent.Greenlet.successful"><tt class="xref py py-meth docutils literal"><span class="pre">successful</span></tt></a>.
The exception instance is available under <a class="reference internal" href="#gevent.Greenlet.value" title="gevent.Greenlet.value"><tt class="xref py py-attr docutils literal"><span class="pre">value</span></tt></a>
property as if it was returned by the greenlet, not raised.</p>
</dd></dl>
</div>
<div class="section" id="spawn-helpers">
<h2>Spawn helpers<a class="headerlink" href="#spawn-helpers" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="gevent.spawn">
<tt class="descclassname">gevent.</tt><tt class="descname">spawn</tt><big>(</big><em>function</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#gevent.spawn" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new <a class="reference internal" href="#gevent.Greenlet" title="gevent.Greenlet"><tt class="xref py py-class docutils literal"><span class="pre">Greenlet</span></tt></a> object and schedule it to run <tt class="docutils literal"><span class="pre">function(*args,</span> <span class="pre">**kwargs)</span></tt>.
This is an alias for <tt class="xref py py-meth docutils literal"><span class="pre">Greenlet.spawn()</span></tt>.</p>
</dd></dl>
<dl class="function">
<dt id="gevent.spawn_later">
<tt class="descclassname">gevent.</tt><tt class="descname">spawn_later</tt><big>(</big><em>seconds</em>, <em>function</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#gevent.spawn_later" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new <a class="reference internal" href="#gevent.Greenlet" title="gevent.Greenlet"><tt class="xref py py-class docutils literal"><span class="pre">Greenlet</span></tt></a> object and schedule it to run <tt class="docutils literal"><span class="pre">function(*args,</span> <span class="pre">**kwargs)</span></tt>
in the future loop iteration <em>seconds</em> later.
This is an alias for <tt class="xref py py-meth docutils literal"><span class="pre">Greenlet.spawn_later()</span></tt>.</p>
</dd></dl>
<dl class="function">
<dt id="gevent.spawn_raw">
<tt class="descclassname">gevent.</tt><tt class="descname">spawn_raw</tt><big>(</big><em>function</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#gevent.spawn_raw" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new <tt class="xref py py-class docutils literal"><span class="pre">greenlet</span></tt> object and schedule it to run <tt class="docutils literal"><span class="pre">function(*args,</span> <span class="pre">**kwargs)</span></tt>.
As this returns a raw greenlet, it does not have all the useful methods that
<a class="reference internal" href="#gevent.Greenlet" title="gevent.Greenlet"><tt class="xref py py-class docutils literal"><span class="pre">gevent.Greenlet</span></tt></a> has and should only be used as an optimization.</p>
</dd></dl>
<dl class="function">
<dt id="gevent.spawn_link">
<tt class="descclassname">gevent.</tt><tt class="descname">spawn_link</tt><big>(</big><em>function</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#gevent.spawn_link" title="Permalink to this definition">¶</a></dt>
<dt id="gevent.spawn_link_value">
<tt class="descclassname">gevent.</tt><tt class="descname">spawn_link_value</tt><big>(</big><em>function</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#gevent.spawn_link_value" title="Permalink to this definition">¶</a></dt>
<dt id="gevent.spawn_link_exception">
<tt class="descclassname">gevent.</tt><tt class="descname">spawn_link_exception</tt><big>(</big><em>function</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#gevent.spawn_link_exception" title="Permalink to this definition">¶</a></dt>
<dd><p>This are the shortcuts for:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">g</span> <span class="o">=</span> <span class="n">spawn</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">g</span><span class="o">.</span><span class="n">link</span><span class="p">()</span> <span class="c"># or g.link_value() or g.link_exception()</span>
</pre></div>
</div>
<p>As <a class="reference internal" href="#gevent.Greenlet.link" title="gevent.Greenlet.link"><tt class="xref py py-meth docutils literal"><span class="pre">Greenlet.link()</span></tt></a> without argument links to the current greenlet, a <tt class="xref py py-class docutils literal"><span class="pre">gevent.greenlet.LinkedExited</span></tt>
exception will be raised if the newly spawned greenlet exits. It is not meant as a way of inter-greenlet communication
but more of a way to assert that a background greenlet is running at least as long as the current greenlet.</p>
<p>See <a class="reference internal" href="#gevent.Greenlet.link" title="gevent.Greenlet.link"><tt class="xref py py-meth docutils literal"><span class="pre">Greenlet.link()</span></tt></a>, <a class="reference internal" href="#gevent.Greenlet.link_value" title="gevent.Greenlet.link_value"><tt class="xref py py-meth docutils literal"><span class="pre">Greenlet.link_value()</span></tt></a> and <a class="reference internal" href="#gevent.Greenlet.link_exception" title="gevent.Greenlet.link_exception"><tt class="xref py py-meth docutils literal"><span class="pre">Greenlet.link_exception()</span></tt></a> for details.</p>
</dd></dl>
</div>
<div class="section" id="useful-general-functions">
<h2>Useful general functions<a class="headerlink" href="#useful-general-functions" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="gevent.getcurrent">
<tt class="descclassname">gevent.</tt><tt class="descname">getcurrent</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.getcurrent" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gevent.sleep">
<tt class="descclassname">gevent.</tt><tt class="descname">sleep</tt><big>(</big><em>seconds=0</em><big>)</big><a class="headerlink" href="#gevent.sleep" title="Permalink to this definition">¶</a></dt>
<dd><p>Put the current greenlet to sleep for at least <em>seconds</em>.</p>
<p><em>seconds</em> may be specified as an integer, or a float if fractional seconds
are desired. Calling sleep with <em>seconds</em> of 0 is the canonical way of
expressing a cooperative yield.</p>
</dd></dl>
<dl class="function">
<dt id="gevent.kill">
<tt class="descclassname">gevent.</tt><tt class="descname">kill</tt><big>(</big><em>greenlet</em>, <em>exception=GreenletExit</em><big>)</big><a class="headerlink" href="#gevent.kill" title="Permalink to this definition">¶</a></dt>
<dd><p>Kill greenlet asynchronously. The current greenlet is not unscheduled.</p>
<p>Note, that <a class="reference internal" href="#gevent.Greenlet.kill" title="gevent.Greenlet.kill"><tt class="xref py py-meth docutils literal"><span class="pre">gevent.Greenlet.kill()</span></tt></a> method does the same and more. However,
MAIN greenlet - the one that exists initially - does not have <tt class="docutils literal"><span class="pre">kill()</span></tt> method
so you have to use this function.</p>
</dd></dl>
<dl class="function">
<dt id="gevent.killall">
<tt class="descclassname">gevent.</tt><tt class="descname">killall</tt><big>(</big><em>greenlets</em>, <em>exception=GreenletExit</em>, <em>block=False</em>, <em>timeout=None</em><big>)</big><a class="headerlink" href="#gevent.killall" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gevent.joinall">
<tt class="descclassname">gevent.</tt><tt class="descname">joinall</tt><big>(</big><em>greenlets</em>, <em>timeout=None</em>, <em>raise_error=False</em><big>)</big><a class="headerlink" href="#gevent.joinall" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gevent.signal">
<tt class="descclassname">gevent.</tt><tt class="descname">signal</tt><big>(</big><em>signalnum</em>, <em>handler</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#gevent.signal" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gevent.fork">
<tt class="descclassname">gevent.</tt><tt class="descname">fork</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.fork" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<dl class="function">
<dt id="gevent.shutdown">
<tt class="descclassname">gevent.</tt><tt class="descname">shutdown</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.shutdown" title="Permalink to this definition">¶</a></dt>
<dd><p>Cancel our CTRL-C handler and wait for core.dispatch() to return.</p>
</dd></dl>
<dl class="function">
<dt id="gevent.reinit">
<tt class="descclassname">gevent.</tt><tt class="descname">reinit</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.reinit" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrapper for <tt class="xref py py-meth docutils literal"><span class="pre">event_reinit()</span></tt>.</p>
</dd></dl>
</div>
<div class="section" id="timeouts">
<h2>Timeouts<a class="headerlink" href="#timeouts" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="gevent.Timeout">
<em class="property">class </em><tt class="descclassname">gevent.</tt><tt class="descname">Timeout</tt><big>(</big><em>seconds=None</em>, <em>exception=None</em><big>)</big><a class="headerlink" href="#gevent.Timeout" title="Permalink to this definition">¶</a></dt>
<dd><p>Raise <em>exception</em> in the current greenlet after given time period:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">timeout</span> <span class="o">=</span> <span class="n">Timeout</span><span class="p">(</span><span class="n">seconds</span><span class="p">,</span> <span class="n">exception</span><span class="p">)</span>
<span class="n">timeout</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="o">...</span> <span class="c"># exception will be raised here, after *seconds* passed since start() call</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">timeout</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
</pre></div>
</div>
<p>When <em>exception</em> is omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, the <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a> instance itself is raised:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">Timeout</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">gevent</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">Timeout</span>: <span class="n">0.1 seconds</span>
</pre></div>
</div>
<p>For Python 2.5 and newer <tt class="docutils literal"><span class="pre">with</span></tt> statement can be used:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">Timeout</span><span class="p">(</span><span class="n">seconds</span><span class="p">,</span> <span class="n">exception</span><span class="p">)</span> <span class="k">as</span> <span class="n">timeout</span><span class="p">:</span>
<span class="k">pass</span> <span class="c"># ... code block ...</span>
</pre></div>
</div>
<p>This is equivalent to try/finally block above with one additional feature:
if <em>exception</em> is <tt class="xref docutils literal"><span class="pre">False</span></tt>, the timeout is still raised, but context manager
suppresses it, so the code outside the with-block won’t see it.</p>
<p>This is handy for adding a timeout to the functions that don’t support <em>timeout</em> parameter themselves:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">data</span> <span class="o">=</span> <span class="bp">None</span>
<span class="k">with</span> <span class="n">Timeout</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="bp">False</span><span class="p">):</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">mysock</span><span class="o">.</span><span class="n">makefile</span><span class="p">()</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="o">...</span> <span class="c"># 5 seconds passed without reading a line</span>
<span class="k">else</span><span class="p">:</span>
<span class="o">...</span> <span class="c"># a line was read within 5 seconds</span>
</pre></div>
</div>
<p>Note that, if <tt class="docutils literal"><span class="pre">readline()</span></tt> above catches and doesn’t re-raise <tt class="xref py py-class docutils literal"><span class="pre">BaseException</span></tt>
(for example, with <tt class="docutils literal"><span class="pre">except:</span></tt>), then your timeout is screwed.</p>
<p>When catching timeouts, keep in mind that the one you catch maybe not the
one you have set; if you going to silent a timeout, always check that it’s
the one you need:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">timeout</span> <span class="o">=</span> <span class="n">Timeout</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">timeout</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="o">...</span>
<span class="k">except</span> <span class="n">Timeout</span><span class="p">,</span> <span class="n">t</span><span class="p">:</span>
<span class="k">if</span> <span class="n">t</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">timeout</span><span class="p">:</span>
<span class="k">raise</span> <span class="c"># not my timeout</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="gevent.Timeout.pending">
<tt class="descname">pending</tt><a class="headerlink" href="#gevent.Timeout.pending" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if the timeout is scheduled to be raised.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Timeout.start">
<tt class="descname">start</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.Timeout.start" title="Permalink to this definition">¶</a></dt>
<dd><p>Schedule the timeout.</p>
</dd></dl>
<dl class="classmethod">
<dt id="gevent.Timeout.start_new">
<em class="property">classmethod </em><tt class="descname">start_new</tt><big>(</big><em>timeout=None</em>, <em>exception=None</em><big>)</big><a class="headerlink" href="#gevent.Timeout.start_new" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a started <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a>.</p>
<p>This is a shortcut, the exact action depends on <em>timeout</em>‘s type:</p>
<ul class="simple">
<li>If <em>timeout</em> is a <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a>, then call its <a class="reference internal" href="#gevent.Timeout.start" title="gevent.Timeout.start"><tt class="xref py py-meth docutils literal"><span class="pre">start()</span></tt></a> method.</li>
<li>Otherwise, create a new <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a> instance, passing (<em>timeout</em>, <em>exception</em>) as
arguments, then call its <a class="reference internal" href="#gevent.Timeout.start" title="gevent.Timeout.start"><tt class="xref py py-meth docutils literal"><span class="pre">start()</span></tt></a> method.</li>
</ul>
<p>Returns the <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a> instance.</p>
</dd></dl>
<dl class="method">
<dt id="gevent.Timeout.cancel">
<tt class="descname">cancel</tt><big>(</big><big>)</big><a class="headerlink" href="#gevent.Timeout.cancel" title="Permalink to this definition">¶</a></dt>
<dd><p>If the timeout is pending, cancel it. Otherwise, do nothing.</p>
</dd></dl>
</dd></dl>
<dl class="function">
<dt id="gevent.with_timeout">
<tt class="descclassname">gevent.</tt><tt class="descname">with_timeout</tt><big>(</big><em>seconds</em>, <em>function</em>, <em>*args</em>, <em>**kwds</em><big>)</big><a class="headerlink" href="#gevent.with_timeout" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrap a call to <em>function</em> with a timeout; if the called
function fails to return before the timeout, cancel it and return a
flag value, provided by <em>timeout_value</em> keyword argument.</p>
<p>If timeout expires but <em>timeout_value</em> is not provided, raise <a class="reference internal" href="#gevent.Timeout" title="gevent.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a>.</p>
<p>Keyword argument <em>timeout_value</em> is not passed to <em>function</em>.</p>
</dd></dl>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="contents.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#"><tt class="docutils literal"><span class="pre">gevent</span></tt> – basic utilities</a><ul>
<li><a class="reference internal" href="#greenlet-objects">Greenlet objects</a></li>
<li><a class="reference internal" href="#spawn-helpers">Spawn helpers</a></li>
<li><a class="reference internal" href="#useful-general-functions">Useful general functions</a></li>
<li><a class="reference internal" href="#timeouts">Timeouts</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="reference.html"
title="previous chapter">API reference</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="networking.html"
title="next chapter">Networking interfaces</a></p>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="networking.html" title="Networking interfaces"
>next</a> |</li>
<li class="right" >
<a href="reference.html" title="API reference"
>previous</a> |</li>
<li><a href="http://www.gevent.org/">Home</a> »</li>
<li><a href="contents.html">Documentation</a> »</li>
<li><a href="reference.html" >API reference</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2009-2010, gevent contributors.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.8.
</div>
</body>
</html>
|