This file is indexed.

/usr/share/doc/python-greenlet-doc/html/greenlet.html is in python-greenlet-doc 0.4.12-2.

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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<!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 &#8212; greenlet 0.4.0 documentation</title>
    <link rel="stylesheet" href="_static/classic.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,
        SOURCELINK_SUFFIX: '.txt'
      };
    </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="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" /> 
  </head>
  <body>
    <div class="related" role="navigation" aria-label="related navigation">
      <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="nav-item nav-item-0"><a href="index.html">greenlet 0.4.0 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <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">
</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 <code class="docutils literal"><span class="pre">test/test_generator.py</span></code>).</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-default"><div class="highlight"><pre><span></span><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="kc">True</span><span class="p">:</span>
        <span class="n">line</span> <span class="o">=</span> <span class="s1">&#39;&#39;</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="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</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="s1">&#39;quit</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">:</span>
            <span class="nb">print</span> <span class="s2">&quot;are you sure?&quot;</span>
            <span class="k">if</span> <span class="n">read_next_char</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">&#39;y&#39;</span><span class="p">:</span>
                <span class="k">continue</span>    <span class="c1"># 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 <code class="docutils literal"><span class="pre">:-)</span></code>]  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-default"><div class="highlight"><pre><span></span>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-default"><div class="highlight"><pre><span></span><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="c1"># 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="c1"># 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="c1"># 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="c1"># 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 <code class="docutils literal"><span class="pre">key</span></code>
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 to 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-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">greenlet</span> <span class="k">import</span> <span class="n">greenlet</span>

<span class="k">def</span> <span class="nf">test1</span><span class="p">():</span>
    <span class="nb">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="nb">print</span> <span class="mi">34</span>

<span class="k">def</span> <span class="nf">test2</span><span class="p">():</span>
    <span class="nb">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="nb">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 <code class="docutils literal"><span class="pre">gr1.switch()</span></code>
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><code class="docutils literal"><span class="pre">greenlet.greenlet</span></code> is the greenlet type, which supports the following
operations:</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">greenlet(run=None,</span> <span class="pre">parent=None)</span></code></dt>
<dd>Create a new greenlet object (without running it).  <code class="docutils literal"><span class="pre">run</span></code> is the
callable to invoke, and <code class="docutils literal"><span class="pre">parent</span></code> is the parent greenlet, which
defaults to the current greenlet.</dd>
<dt><code class="docutils literal"><span class="pre">greenlet.getcurrent()</span></code></dt>
<dd>Returns the current greenlet (i.e. the one which called this
function).</dd>
<dt><code class="docutils literal"><span class="pre">greenlet.GreenletExit</span></code></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 <code class="docutils literal"><span class="pre">greenlet</span></code> type can be subclassed, too.  A greenlet runs by calling
its <code class="docutils literal"><span class="pre">run</span></code> attribute, which is normally set when the greenlet is
created; but for subclasses it also makes sense to define a <code class="docutils literal"><span class="pre">run</span></code> method
instead of giving a <code class="docutils literal"><span class="pre">run</span></code> 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-default"><div class="highlight"><pre><span></span><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="nb">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="nb">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="s2">&quot;hello&quot;</span><span class="p">,</span> <span class="s2">&quot; world&quot;</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><code class="docutils literal"><span class="pre">g.switch(*args,</span> <span class="pre">**kwargs)</span></code></dt>
<dd>Switches execution to the greenlet <code class="docutils literal"><span class="pre">g</span></code>, sending it the given
arguments.  As a special case, if <code class="docutils literal"><span class="pre">g</span></code> did not start yet, then it
will start to run now.</dd>
<dt>Dying greenlet</dt>
<dd>If a greenlet’s <code class="docutils literal"><span class="pre">run()</span></code> finishes, its return value is the object
sent to its parent.  If <code class="docutils literal"><span class="pre">run()</span></code> terminates with an exception, the
exception is propagated to its parent (unless it is a
<code class="docutils literal"><span class="pre">greenlet.GreenletExit</span></code> 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 <code class="docutils literal"><span class="pre">switch()</span></code> in
which it was previously suspended.  Indeed, although a call to
<code class="docutils literal"><span class="pre">switch()</span></code> 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 <code class="docutils literal"><span class="pre">switch()</span></code> where it was
suspended, and the <code class="docutils literal"><span class="pre">switch()</span></code> itself appears to return the object that
was just sent.  This means that <code class="docutils literal"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">g.switch(y)</span></code> will send the object
<code class="docutils literal"><span class="pre">y</span></code> to <code class="docutils literal"><span class="pre">g</span></code>, and will later put the (unrelated) object that some
(unrelated) greenlet passes back to us into <code class="docutils literal"><span class="pre">x</span></code>.</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><code class="docutils literal"><span class="pre">g.switch(*args,</span> <span class="pre">**kwargs)</span></code></dt>
<dd>Switches execution to the greenlet <code class="docutils literal"><span class="pre">g</span></code>.  See above.</dd>
<dt><code class="docutils literal"><span class="pre">g.run</span></code></dt>
<dd>The callable that <code class="docutils literal"><span class="pre">g</span></code> will run when it starts.  After <code class="docutils literal"><span class="pre">g</span></code> started,
this attribute no longer exists.</dd>
<dt><code class="docutils literal"><span class="pre">g.parent</span></code></dt>
<dd>The parent greenlet.  This is writeable, but it is not allowed to
create cycles of parents.</dd>
<dt><code class="docutils literal"><span class="pre">g.gr_frame</span></code></dt>
<dd>The current top frame, or None.</dd>
<dt><code class="docutils literal"><span class="pre">g.dead</span></code></dt>
<dd>True if <code class="docutils literal"><span class="pre">g</span></code> is dead (i.e. it finished its execution).</dd>
<dt><code class="docutils literal"><span class="pre">bool(g)</span></code></dt>
<dd>True if <code class="docutils literal"><span class="pre">g</span></code> is active, False if it is dead or not yet started.</dd>
<dt><code class="docutils literal"><span class="pre">g.throw([typ,</span> <span class="pre">[val,</span> <span class="pre">[tb]]])</span></code></dt>
<dd><p class="first">Switches execution to the greenlet <code class="docutils literal"><span class="pre">g</span></code>, but immediately raises the
given exception in <code class="docutils literal"><span class="pre">g</span></code>.  If no argument is provided, the exception
defaults to <code class="docutils literal"><span class="pre">greenlet.GreenletExit</span></code>.  The normal exception
propagation rules apply, as described above.  Note that calling this
method is almost equivalent to the following:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><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
<code class="docutils literal"><span class="pre">greenlet.GreenletExit</span></code> exception, which would not propagate
from <code class="docutils literal"><span class="pre">g_raiser</span></code> to <code class="docutils literal"><span class="pre">g</span></code>.</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
<code class="docutils literal"><span class="pre">try:finally:</span></code> 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 class="section" id="tracing-support">
<h3>Tracing support<a class="headerlink" href="#tracing-support" title="Permalink to this headline"></a></h3>
<p>Standard Python tracing and profiling doesn’t work as expected when used with
greenlet since stack and frame switching happens on the same Python thread.
It is difficult to detect greenlet switching reliably with conventional
methods, so to improve support for debugging, tracing and profiling greenlet
based code there are new functions in the greenlet module:</p>
<dl class="docutils">
<dt><code class="docutils literal"><span class="pre">greenlet.gettrace()</span></code></dt>
<dd>Returns a previously set tracing function, or None.</dd>
<dt><code class="docutils literal"><span class="pre">greenlet.settrace(callback)</span></code></dt>
<dd><p class="first">Sets a new tracing function and returns a previous tracing function, or
None. The callback is called on various events and is expected to have
the following signature:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">callback</span><span class="p">(</span><span class="n">event</span><span class="p">,</span> <span class="n">args</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">event</span> <span class="o">==</span> <span class="s1">&#39;switch&#39;</span><span class="p">:</span>
        <span class="n">origin</span><span class="p">,</span> <span class="n">target</span> <span class="o">=</span> <span class="n">args</span>
        <span class="c1"># Handle a switch from origin to target.</span>
        <span class="c1"># Note that callback is running in the context of target</span>
        <span class="c1"># greenlet and any exceptions will be passed as if</span>
        <span class="c1"># target.throw() was used instead of a switch.</span>
        <span class="k">return</span>
    <span class="k">if</span> <span class="n">event</span> <span class="o">==</span> <span class="s1">&#39;throw&#39;</span><span class="p">:</span>
        <span class="n">origin</span><span class="p">,</span> <span class="n">target</span> <span class="o">=</span> <span class="n">args</span>
        <span class="c1"># Handle a throw from origin to target.</span>
        <span class="c1"># Note that callback is running in the context of target</span>
        <span class="c1"># greenlet and any exceptions will replace the original, as</span>
        <span class="c1"># if target.throw() was used with the replacing exception.</span>
        <span class="k">return</span>
</pre></div>
</div>
<p class="last">For compatibility it is very important to unpack args tuple only when
event is either <code class="docutils literal"><span class="pre">'switch'</span></code> or <code class="docutils literal"><span class="pre">'throw'</span></code> and not when <code class="docutils literal"><span class="pre">event</span></code> is
potentially something else. This way API can be extended to new events
similar to <code class="docutils literal"><span class="pre">sys.settrace()</span></code>.</p>
</dd>
</dl>
</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 <code class="docutils literal"><span class="pre">greenlet.h</span></code> 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><code class="docutils literal"><span class="pre">PyGreenlet_Import()</span></code></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><code class="docutils literal"><span class="pre">int</span> <span class="pre">PyGreenlet_Check(PyObject</span> <span class="pre">*p)</span></code></dt>
<dd>Macro that returns true if the argument is a PyGreenlet.</dd>
<dt><code class="docutils literal"><span class="pre">int</span> <span class="pre">PyGreenlet_STARTED(PyGreenlet</span> <span class="pre">*g)</span></code></dt>
<dd>Macro that returns true if the greenlet <code class="docutils literal"><span class="pre">g</span></code> has started.</dd>
<dt><code class="docutils literal"><span class="pre">int</span> <span class="pre">PyGreenlet_ACTIVE(PyGreenlet</span> <span class="pre">*g)</span></code></dt>
<dd>Macro that returns true if the greenlet <code class="docutils literal"><span class="pre">g</span></code> has started and has not died.</dd>
<dt><code class="docutils literal"><span class="pre">PyGreenlet</span> <span class="pre">*PyGreenlet_GET_PARENT(PyGreenlet</span> <span class="pre">*g)</span></code></dt>
<dd>Macro that returns the parent greenlet of <code class="docutils literal"><span class="pre">g</span></code>.</dd>
<dt><code class="docutils literal"><span class="pre">int</span> <span class="pre">PyGreenlet_SetParent(PyGreenlet</span> <span class="pre">*g,</span> <span class="pre">PyGreenlet</span> <span class="pre">*nparent)</span></code></dt>
<dd>Set the parent greenlet of <code class="docutils literal"><span class="pre">g</span></code>. Returns 0 for success. If -1 is returned,
then <code class="docutils literal"><span class="pre">g</span></code> is not a pointer to a PyGreenlet, and an AttributeError will
be raised.</dd>
<dt><code class="docutils literal"><span class="pre">PyGreenlet</span> <span class="pre">*PyGreenlet_GetCurrent(void)</span></code></dt>
<dd>Returns the currently active greenlet object.</dd>
<dt><code 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></code></dt>
<dd>Creates a new greenlet object with the callable <code class="docutils literal"><span class="pre">run</span></code> and parent
<code class="docutils literal"><span class="pre">parent</span></code>. Both parameters are optional. If <code class="docutils literal"><span class="pre">run</span></code> is NULL, then the
greenlet will be created, but will fail if switched in. If <code class="docutils literal"><span class="pre">parent</span></code> is
NULL, the parent is automatically set to the current greenlet.</dd>
<dt><code class="docutils literal"><span class="pre">PyObject</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></code></dt>
<dd>Switches to the greenlet <code class="docutils literal"><span class="pre">g</span></code>. <code class="docutils literal"><span class="pre">args</span></code> and <code class="docutils literal"><span class="pre">kwargs</span></code> are optional and
can be NULL. If <code class="docutils literal"><span class="pre">args</span></code> 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, <code class="docutils literal"><span class="pre">args</span></code> should be a tuple and
<code class="docutils literal"><span class="pre">kwargs</span></code> should be a dict.</dd>
<dt><code 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></code></dt>
<dd>Switches to greenlet <code class="docutils literal"><span class="pre">g</span></code>, but immediately raise an exception of type
<code class="docutils literal"><span class="pre">typ</span></code> with the value <code class="docutils literal"><span class="pre">val</span></code>, and optionally, the traceback object
<code class="docutils literal"><span class="pre">tb</span></code>. <code class="docutils literal"><span class="pre">tb</span></code> 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"><span class="std std-ref">Search Page</span></a></li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <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>
<li><a class="reference internal" href="#tracing-support">Tracing support</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>

  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/greenlet.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="nav-item nav-item-0"><a href="index.html">greenlet 0.4.0 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2017, Armin Rigo, Christian Tismer.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.5.
    </div>
  </body>
</html>