/usr/share/doc/python-greenlet-doc/html/greenlet.html is in python-greenlet-doc 0.4.2-1build1.
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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | <!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>greenlet: Lightweight concurrent programming — greenlet 0.4.0 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.4.0',
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="greenlet 0.4.0 documentation" href="index.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><a href="index.html">greenlet 0.4.0 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="greenlet-lightweight-concurrent-programming">
<h1>greenlet: Lightweight concurrent programming<a class="headerlink" href="#greenlet-lightweight-concurrent-programming" title="Permalink to this headline">¶</a></h1>
<div class="toctree-wrapper compound">
<ul class="simple">
</ul>
</div>
<div class="section" id="motivation">
<h2>Motivation<a class="headerlink" href="#motivation" title="Permalink to this headline">¶</a></h2>
<p>The “greenlet” package is a spin-off of <a class="reference external" href="http://www.stackless.com">Stackless</a>, a version of CPython
that supports micro-threads called “tasklets”. Tasklets run
pseudo-concurrently (typically in a single or a few OS-level threads) and
are synchronized with data exchanges on “channels”.</p>
<p>A “greenlet”, on the other hand, is a still more primitive notion of
micro-thread with no implicit scheduling; coroutines, in other words.
This is useful when you want to
control exactly when your code runs. You can build custom scheduled
micro-threads on top of greenlet; however, it seems that greenlets are
useful on their own as a way to make advanced control flow structures.
For example, we can recreate generators; the difference with Python’s own
generators is that our generators can call nested functions and the nested
functions can yield values too. (Additionally, you don’t need a “yield”
keyword. See the example in <tt class="docutils literal"><span class="pre">test/test_generator.py</span></tt>).</p>
<p>Greenlets are provided as a C extension module for the regular unmodified
interpreter.</p>
<div class="section" id="example">
<h3>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
<p>Let’s consider a system controlled by a terminal-like console, where the user
types commands. Assume that the input comes character by character. In such
a system, there will typically be a loop like the following one:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">process_commands</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="s">''</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">):</span>
<span class="n">line</span> <span class="o">+=</span> <span class="n">read_next_char</span><span class="p">()</span>
<span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="s">'quit</span><span class="se">\n</span><span class="s">'</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"are you sure?"</span>
<span class="k">if</span> <span class="n">read_next_char</span><span class="p">()</span> <span class="o">!=</span> <span class="s">'y'</span><span class="p">:</span>
<span class="k">continue</span> <span class="c"># ignore the command</span>
<span class="n">process_command</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</div>
<p>Now assume that you want to plug this program into a GUI. Most GUI toolkits
are event-based. They will invoke a call-back for each character the user
presses. [Replace “GUI” with “XML expat parser” if that rings more bells to
you <tt class="docutils literal"><span class="pre">:-)</span></tt>] In this setting, it is difficult to implement the
read_next_char() function needed by the code above. We have two incompatible
functions:</p>
<div class="highlight-python"><div class="highlight"><pre>def event_keydown(key):
??
def read_next_char():
?? should wait for the next event_keydown() call
</pre></div>
</div>
<p>You might consider doing that with threads. Greenlets are an alternate
solution that don’t have the related locking and shutdown problems. You
start the process_commands() function in its own, separate greenlet, and
then you exchange the keypresses with it as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">event_keydown</span><span class="p">(</span><span class="n">key</span><span class="p">):</span>
<span class="c"># jump into g_processor, sending it the key</span>
<span class="n">g_processor</span><span class="o">.</span><span class="n">switch</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">read_next_char</span><span class="p">():</span>
<span class="c"># g_self is g_processor in this simple example</span>
<span class="n">g_self</span> <span class="o">=</span> <span class="n">greenlet</span><span class="o">.</span><span class="n">getcurrent</span><span class="p">()</span>
<span class="c"># jump to the parent (main) greenlet, waiting for the next key</span>
<span class="n">next_char</span> <span class="o">=</span> <span class="n">g_self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">switch</span><span class="p">()</span>
<span class="k">return</span> <span class="n">next_char</span>
<span class="n">g_processor</span> <span class="o">=</span> <span class="n">greenlet</span><span class="p">(</span><span class="n">process_commands</span><span class="p">)</span>
<span class="n">g_processor</span><span class="o">.</span><span class="n">switch</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span> <span class="c"># input arguments to process_commands()</span>
<span class="n">gui</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
</pre></div>
</div>
<p>In this example, the execution flow is: when read_next_char() is called, it
is part of the g_processor greenlet, so when it switches to its parent
greenlet, it resumes execution in the top-level main loop (the GUI). When
the GUI calls event_keydown(), it switches to g_processor, which means that
the execution jumps back wherever it was suspended in that greenlet – in
this case, to the switch() instruction in read_next_char() – and the <tt class="docutils literal"><span class="pre">key</span></tt>
argument in event_keydown() is passed as the return value of the switch() in
read_next_char().</p>
<p>Note that read_next_char() will be suspended and resumed with its call stack
preserved, so that it will itself return to different positions in
process_commands() depending on where it was originally called from. This
allows the logic of the program to be kept in a nice control-flow way; we
don’t have to completely rewrite process_commands() to turn it into a state
machine.</p>
</div>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h2>
<div class="section" id="introduction">
<h3>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h3>
<p>A “greenlet” is a small independent pseudo-thread. Think about it as a
small stack of frames; the outermost (bottom) frame is the initial
function you called, and the innermost frame is the one in which the
greenlet is currently paused. You work with greenlets by creating a
number of such stacks and jumping execution between them. Jumps are never
implicit: a greenlet must choose to jump to another greenlet, which will
cause the former to suspend and the latter to resume where it was
suspended. Jumping between greenlets is called “switching”.</p>
<p>When you create a greenlet, it gets an initially empty stack; when you
first switch to it, it starts the run a specified function, which may call
other functions, switch out of the greenlet, etc. When eventually the
outermost function finishes its execution, the greenlet’s stack becomes
empty again and the greenlet is “dead”. Greenlets can also die of an
uncaught exception.</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">greenlet</span> <span class="kn">import</span> <span class="n">greenlet</span>
<span class="k">def</span> <span class="nf">test1</span><span class="p">():</span>
<span class="k">print</span> <span class="mi">12</span>
<span class="n">gr2</span><span class="o">.</span><span class="n">switch</span><span class="p">()</span>
<span class="k">print</span> <span class="mi">34</span>
<span class="k">def</span> <span class="nf">test2</span><span class="p">():</span>
<span class="k">print</span> <span class="mi">56</span>
<span class="n">gr1</span><span class="o">.</span><span class="n">switch</span><span class="p">()</span>
<span class="k">print</span> <span class="mi">78</span>
<span class="n">gr1</span> <span class="o">=</span> <span class="n">greenlet</span><span class="p">(</span><span class="n">test1</span><span class="p">)</span>
<span class="n">gr2</span> <span class="o">=</span> <span class="n">greenlet</span><span class="p">(</span><span class="n">test2</span><span class="p">)</span>
<span class="n">gr1</span><span class="o">.</span><span class="n">switch</span><span class="p">()</span>
</pre></div>
</div>
<p>The last line jumps to test1, which prints 12, jumps to test2, prints 56,
jumps back into test1, prints 34; and then test1 finishes and gr1 dies.
At this point, the execution comes back to the original <tt class="docutils literal"><span class="pre">gr1.switch()</span></tt>
call. Note that 78 is never printed.</p>
</div>
<div class="section" id="parents">
<h3>Parents<a class="headerlink" href="#parents" title="Permalink to this headline">¶</a></h3>
<p>Let’s see where execution goes when a greenlet dies. Every greenlet has a
“parent” greenlet. The parent greenlet is initially the one in which the
greenlet was created (this can be changed at any time). The parent is
where execution continues when a greenlet dies. This way, greenlets are
organized in a tree. Top-level code that doesn’t run in a user-created
greenlet runs in the implicit “main” greenlet, which is the root of the
tree.</p>
<p>In the above example, both gr1 and gr2 have the main greenlet as a parent.
Whenever one of them dies, the execution comes back to “main”.</p>
<p>Uncaught exceptions are propagated into the parent, too. For example, if
the above test2() contained a typo, it would generate a NameError that
would kill gr2, and the exception would go back directly into “main”.
The traceback would show test2, but not test1. Remember, switches are not
calls, but transfer of execution between parallel “stack containers”, and
the “parent” defines which stack logically comes “below” the current one.</p>
</div>
<div class="section" id="instantiation">
<h3>Instantiation<a class="headerlink" href="#instantiation" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">greenlet.greenlet</span></tt> is the greenlet type, which supports the following
operations:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">greenlet(run=None,</span> <span class="pre">parent=None)</span></tt></dt>
<dd>Create a new greenlet object (without running it). <tt class="docutils literal"><span class="pre">run</span></tt> is the
callable to invoke, and <tt class="docutils literal"><span class="pre">parent</span></tt> is the parent greenlet, which
defaults to the current greenlet.</dd>
<dt><tt class="docutils literal"><span class="pre">greenlet.getcurrent()</span></tt></dt>
<dd>Returns the current greenlet (i.e. the one which called this
function).</dd>
<dt><tt class="docutils literal"><span class="pre">greenlet.GreenletExit</span></tt></dt>
<dd>This special exception does not propagate to the parent greenlet; it
can be used to kill a single greenlet.</dd>
</dl>
<p>The <tt class="docutils literal"><span class="pre">greenlet</span></tt> type can be subclassed, too. A greenlet runs by calling
its <tt class="docutils literal"><span class="pre">run</span></tt> attribute, which is normally set when the greenlet is
created; but for subclasses it also makes sense to define a <tt class="docutils literal"><span class="pre">run</span></tt> method
instead of giving a <tt class="docutils literal"><span class="pre">run</span></tt> argument to the constructor.</p>
</div>
<div class="section" id="switching">
<h3>Switching<a class="headerlink" href="#switching" title="Permalink to this headline">¶</a></h3>
<p>Switches between greenlets occur when the method switch() of a greenlet is
called, in which case execution jumps to the greenlet whose switch() is
called, or when a greenlet dies, in which case execution jumps to the
parent greenlet. During a switch, an object or an exception is “sent” to
the target greenlet; this can be used as a convenient way to pass
information between greenlets. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test1</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">gr2</span><span class="o">.</span><span class="n">switch</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">)</span>
<span class="k">print</span> <span class="n">z</span>
<span class="k">def</span> <span class="nf">test2</span><span class="p">(</span><span class="n">u</span><span class="p">):</span>
<span class="k">print</span> <span class="n">u</span>
<span class="n">gr1</span><span class="o">.</span><span class="n">switch</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="n">gr1</span> <span class="o">=</span> <span class="n">greenlet</span><span class="p">(</span><span class="n">test1</span><span class="p">)</span>
<span class="n">gr2</span> <span class="o">=</span> <span class="n">greenlet</span><span class="p">(</span><span class="n">test2</span><span class="p">)</span>
<span class="n">gr1</span><span class="o">.</span><span class="n">switch</span><span class="p">(</span><span class="s">"hello"</span><span class="p">,</span> <span class="s">" world"</span><span class="p">)</span>
</pre></div>
</div>
<p>This prints “hello world” and 42, with the same order of execution as the
previous example. Note that the arguments of test1() and test2() are not
provided when the greenlet is created, but only the first time someone
switches to it.</p>
<p>Here are the precise rules for sending objects around:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">g.switch(*args,</span> <span class="pre">**kwargs)</span></tt></dt>
<dd>Switches execution to the greenlet <tt class="docutils literal"><span class="pre">g</span></tt>, sending it the given
arguments. As a special case, if <tt class="docutils literal"><span class="pre">g</span></tt> did not start yet, then it
will start to run now.</dd>
<dt>Dying greenlet</dt>
<dd>If a greenlet’s <tt class="docutils literal"><span class="pre">run()</span></tt> finishes, its return value is the object
sent to its parent. If <tt class="docutils literal"><span class="pre">run()</span></tt> terminates with an exception, the
exception is propagated to its parent (unless it is a
<tt class="docutils literal"><span class="pre">greenlet.GreenletExit</span></tt> exception, in which case the exception
object is caught and <em>returned</em> to the parent).</dd>
</dl>
<p>Apart from the cases described above, the target greenlet normally
receives the object as the return value of the call to <tt class="docutils literal"><span class="pre">switch()</span></tt> in
which it was previously suspended. Indeed, although a call to
<tt class="docutils literal"><span class="pre">switch()</span></tt> does not return immediately, it will still return at some
point in the future, when some other greenlet switches back. When this
occurs, then execution resumes just after the <tt class="docutils literal"><span class="pre">switch()</span></tt> where it was
suspended, and the <tt class="docutils literal"><span class="pre">switch()</span></tt> itself appears to return the object that
was just sent. This means that <tt class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">g.switch(y)</span></tt> will send the object
<tt class="docutils literal"><span class="pre">y</span></tt> to <tt class="docutils literal"><span class="pre">g</span></tt>, and will later put the (unrelated) object that some
(unrelated) greenlet passes back to us into <tt class="docutils literal"><span class="pre">x</span></tt>.</p>
<p>Note that any attempt to switch to a dead greenlet actually goes to the
dead greenlet’s parent, or its parent’s parent, and so on. (The final
parent is the “main” greenlet, which is never dead.)</p>
</div>
<div class="section" id="methods-and-attributes-of-greenlets">
<h3>Methods and attributes of greenlets<a class="headerlink" href="#methods-and-attributes-of-greenlets" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">g.switch(*args,</span> <span class="pre">**kwargs)</span></tt></dt>
<dd>Switches execution to the greenlet <tt class="docutils literal"><span class="pre">g</span></tt>. See above.</dd>
<dt><tt class="docutils literal"><span class="pre">g.run</span></tt></dt>
<dd>The callable that <tt class="docutils literal"><span class="pre">g</span></tt> will run when it starts. After <tt class="docutils literal"><span class="pre">g</span></tt> started,
this attribute no longer exists.</dd>
<dt><tt class="docutils literal"><span class="pre">g.parent</span></tt></dt>
<dd>The parent greenlet. This is writeable, but it is not allowed to
create cycles of parents.</dd>
<dt><tt class="docutils literal"><span class="pre">g.gr_frame</span></tt></dt>
<dd>The current top frame, or None.</dd>
<dt><tt class="docutils literal"><span class="pre">g.dead</span></tt></dt>
<dd>True if <tt class="docutils literal"><span class="pre">g</span></tt> is dead (i.e. it finished its execution).</dd>
<dt><tt class="docutils literal"><span class="pre">bool(g)</span></tt></dt>
<dd>True if <tt class="docutils literal"><span class="pre">g</span></tt> is active, False if it is dead or not yet started.</dd>
<dt><tt class="docutils literal"><span class="pre">g.throw([typ,</span> <span class="pre">[val,</span> <span class="pre">[tb]]])</span></tt></dt>
<dd><p class="first">Switches execution to the greenlet <tt class="docutils literal"><span class="pre">g</span></tt>, but immediately raises the
given exception in <tt class="docutils literal"><span class="pre">g</span></tt>. If no argument is provided, the exception
defaults to <tt class="docutils literal"><span class="pre">greenlet.GreenletExit</span></tt>. The normal exception
propagation rules apply, as described above. Note that calling this
method is almost equivalent to the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">raiser</span><span class="p">():</span>
<span class="k">raise</span> <span class="n">typ</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">tb</span>
<span class="n">g_raiser</span> <span class="o">=</span> <span class="n">greenlet</span><span class="p">(</span><span class="n">raiser</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">g</span><span class="p">)</span>
<span class="n">g_raiser</span><span class="o">.</span><span class="n">switch</span><span class="p">()</span>
</pre></div>
</div>
<p class="last">except that this trick does not work for the
<tt class="docutils literal"><span class="pre">greenlet.GreenletExit</span></tt> exception, which would not propagate
from <tt class="docutils literal"><span class="pre">g_raiser</span></tt> to <tt class="docutils literal"><span class="pre">g</span></tt>.</p>
</dd>
</dl>
</div>
<div class="section" id="greenlets-and-python-threads">
<h3>Greenlets and Python threads<a class="headerlink" href="#greenlets-and-python-threads" title="Permalink to this headline">¶</a></h3>
<p>Greenlets can be combined with Python threads; in this case, each thread
contains an independent “main” greenlet with a tree of sub-greenlets. It
is not possible to mix or switch between greenlets belonging to different
threads.</p>
</div>
<div class="section" id="garbage-collecting-live-greenlets">
<h3>Garbage-collecting live greenlets<a class="headerlink" href="#garbage-collecting-live-greenlets" title="Permalink to this headline">¶</a></h3>
<p>If all the references to a greenlet object go away (including the
references from the parent attribute of other greenlets), then there is no
way to ever switch back to this greenlet. In this case, a GreenletExit
exception is generated into the greenlet. This is the only case where a
greenlet receives the execution asynchronously. This gives
<tt class="docutils literal"><span class="pre">try:finally:</span></tt> blocks a chance to clean up resources held by the
greenlet. This feature also enables a programming style in which
greenlets are infinite loops waiting for data and processing it. Such
loops are automatically interrupted when the last reference to the
greenlet goes away.</p>
<p>The greenlet is expected to either die or be resurrected by having a new
reference to it stored somewhere; just catching and ignoring the
GreenletExit is likely to lead to an infinite loop.</p>
<p>Greenlets do not participate in garbage collection; cycles involving data
that is present in a greenlet’s frames will not be detected. Storing
references to other greenlets cyclically may lead to leaks.</p>
</div>
</div>
<div class="section" id="c-api-reference">
<h2>C API Reference<a class="headerlink" href="#c-api-reference" title="Permalink to this headline">¶</a></h2>
<p>Greenlets can be created and manipulated from extension modules written in C or
C++, or from applications that embed Python. The <tt class="docutils literal"><span class="pre">greenlet.h</span></tt> header is
provided, and exposes the entire API available to pure Python modules.</p>
<div class="section" id="types">
<h3>Types<a class="headerlink" href="#types" title="Permalink to this headline">¶</a></h3>
<table border="1" class="docutils">
<colgroup>
<col width="51%" />
<col width="49%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Type name</th>
<th class="head">Python name</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>PyGreenlet</td>
<td>greenlet.greenlet</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="exceptions">
<h3>Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h3>
<table border="1" class="docutils">
<colgroup>
<col width="48%" />
<col width="52%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Type name</th>
<th class="head">Python name</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>PyExc_GreenletError</td>
<td>greenlet.error</td>
</tr>
<tr class="row-odd"><td>PyExc_GreenletExit</td>
<td>greenlet.GreenletExit</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="reference">
<h3>Reference<a class="headerlink" href="#reference" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">PyGreenlet_Import()</span></tt></dt>
<dd>A macro that imports the greenlet module and initializes the C API. This
must be called once for each extension module that uses the greenlet C API.</dd>
<dt><tt class="docutils literal"><span class="pre">int</span> <span class="pre">PyGreenlet_Check(PyObject</span> <span class="pre">*p)</span></tt></dt>
<dd>Macro that returns true if the argument is a PyGreenlet.</dd>
<dt><tt class="docutils literal"><span class="pre">int</span> <span class="pre">PyGreenlet_STARTED(PyGreenlet</span> <span class="pre">*g)</span></tt></dt>
<dd>Macro that returns true if the greenlet <tt class="docutils literal"><span class="pre">g</span></tt> has started.</dd>
<dt><tt class="docutils literal"><span class="pre">int</span> <span class="pre">PyGreenlet_ACTIVE(PyGreenlet</span> <span class="pre">*g)</span></tt></dt>
<dd>Macro that returns true if the greenlet <tt class="docutils literal"><span class="pre">g</span></tt> has started and has not died.</dd>
<dt><tt class="docutils literal"><span class="pre">PyGreenlet</span> <span class="pre">*PyGreenlet_GET_PARENT(PyGreenlet</span> <span class="pre">*g)</span></tt></dt>
<dd>Macro that returns the parent greenlet of <tt class="docutils literal"><span class="pre">g</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">PyGreenlet</span> <span class="pre">*PyGreenlet_SetParent(PyGreenlet</span> <span class="pre">*g)</span></tt></dt>
<dd>Set the parent greenlet of <tt class="docutils literal"><span class="pre">g</span></tt>. Returns 0 for success. If -1 is returned,
then <tt class="docutils literal"><span class="pre">g</span></tt> is not a pointer to a PyGreenlet, and an AttributeError will
be raised.</dd>
<dt><tt class="docutils literal"><span class="pre">PyGreenlet</span> <span class="pre">*PyGreenlet_GetCurrent(void)</span></tt></dt>
<dd>Returns the currently active greenlet object.</dd>
<dt><tt class="docutils literal"><span class="pre">PyGreenlet</span> <span class="pre">*PyGreenlet_New(PyObject</span> <span class="pre">*run,</span> <span class="pre">PyObject</span> <span class="pre">*parent)</span></tt></dt>
<dd>Creates a new greenlet object with the callable <tt class="docutils literal"><span class="pre">run</span></tt> and parent
<tt class="docutils literal"><span class="pre">parent</span></tt>. Both parameters are optional. If <tt class="docutils literal"><span class="pre">run</span></tt> is NULL, then the
greenlet will be created, but will fail if switched in. If <tt class="docutils literal"><span class="pre">parent</span></tt> is
NULL, the parent is automatically set to the current greenlet.</dd>
<dt><tt class="docutils literal"><span class="pre">PyGreenlet</span> <span class="pre">*PyGreenlet_Switch(PyGreenlet</span> <span class="pre">*g,</span> <span class="pre">PyObject</span> <span class="pre">*args,</span> <span class="pre">PyObject</span> <span class="pre">*kwargs)</span></tt></dt>
<dd>Switches to the greenlet <tt class="docutils literal"><span class="pre">g</span></tt>. <tt class="docutils literal"><span class="pre">args</span></tt> and <tt class="docutils literal"><span class="pre">kwargs</span></tt> are optional and
can be NULL. If <tt class="docutils literal"><span class="pre">args</span></tt> is NULL, an empty tuple is passed to the target
greenlet. If kwargs is NULL, no keyword arguments are passed to the target
greenlet. If arguments are specified, <tt class="docutils literal"><span class="pre">args</span></tt> should be a tuple and
<tt class="docutils literal"><span class="pre">kwargs</span></tt> should be a dict.</dd>
<dt><tt class="docutils literal"><span class="pre">PyObject</span> <span class="pre">*PyGreenlet_Throw(PyGreenlet</span> <span class="pre">*g,</span> <span class="pre">PyObject</span> <span class="pre">*typ,</span> <span class="pre">PyObject</span> <span class="pre">*val,</span> <span class="pre">PyObject</span> <span class="pre">*tb)</span></tt></dt>
<dd>Switches to greenlet <tt class="docutils literal"><span class="pre">g</span></tt>, but immediately raise an exception of type
<tt class="docutils literal"><span class="pre">typ</span></tt> with the value <tt class="docutils literal"><span class="pre">val</span></tt>, and optionally, the traceback object
<tt class="docutils literal"><span class="pre">tb</span></tt>. <tt class="docutils literal"><span class="pre">tb</span></tt> can be NULL.</dd>
</dl>
</div>
</div>
<div class="section" id="indices-and-tables">
<h2>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference internal" href="search.html"><em>Search Page</em></a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">greenlet: Lightweight concurrent programming</a><ul>
<li><a class="reference internal" href="#motivation">Motivation</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
<li><a class="reference internal" href="#usage">Usage</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#parents">Parents</a></li>
<li><a class="reference internal" href="#instantiation">Instantiation</a></li>
<li><a class="reference internal" href="#switching">Switching</a></li>
<li><a class="reference internal" href="#methods-and-attributes-of-greenlets">Methods and attributes of greenlets</a></li>
<li><a class="reference internal" href="#greenlets-and-python-threads">Greenlets and Python threads</a></li>
<li><a class="reference internal" href="#garbage-collecting-live-greenlets">Garbage-collecting live greenlets</a></li>
</ul>
</li>
<li><a class="reference internal" href="#c-api-reference">C API Reference</a><ul>
<li><a class="reference internal" href="#types">Types</a></li>
<li><a class="reference internal" href="#exceptions">Exceptions</a></li>
<li><a class="reference internal" href="#reference">Reference</a></li>
</ul>
</li>
<li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/greenlet.txt"
rel="nofollow">Show Source</a></li>
</ul>
<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><a href="index.html">greenlet 0.4.0 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2011, Armin Rigo, Christian Tismer.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>
|