<!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 v0.3.3 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.3.3', 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 v0.3.3 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 v0.3.3 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"><pre>def event_keydown(key): ?? def read_next_char(): ?? should wait for the next event_keydown() call</pre> </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><th class="head">Type name</th> <th class="head">Python name</th> </tr> </thead> <tbody valign="top"> <tr><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><th class="head">Type name</th> <th class="head">Python name</th> </tr> </thead> <tbody valign="top"> <tr><td>PyExc_GreenletError</td> <td>greenlet.error</td> </tr> <tr><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" size="18" /> <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 v0.3.3 documentation</a> »</li> </ul> </div> <div class="footer"> © Copyright 2011, Armin Rigo, Christian Tismer. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7. </div> </body> </html>