/usr/share/doc/python-kiwi/api/kiwi.tasklet.html is in python-kiwi 1.9.22-4.
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 | <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html>
<head>
<title>kiwi.tasklet : API documentation</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="apidocs.css" />
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">
Kiwi API Documentation
</a>
</div>
</div>
</nav>
<div id="showPrivate">
<button class="btn btn-link" onclick="togglePrivate()">Toggle Private API</button>
</div>
<div class="container">
<div class="page-header">
<h1 class="module"><code>kiwi.tasklet</code> <small>module documentation</small></h1>
<span id="partOf">
Part of <code><a href="kiwi.html" data-type="Package" class="code">kiwi</a></code>
</span>
</div>
<div class="extrasDocstring">
</div>
<div class="moduleDocstring">
<div><p>Pseudo-thread (coroutines) framework</p>
<h1 class="heading">Introduction</h1>
<p>This module adds infrastructure for managing tasklets. In this
context, a tasklet is defined as a routine that explicitly gives back
control to the main program a certain points in the code, while waiting
for certain events. Other terms that may be used to describe tasklets
include <i>coroutines</i>, or <i>cooperative threads</i>.</p>
<p>The main advantages of tasklets are:</p>
<ul>
<li>
Eliminates the danger of unexpected race conditions or deadlocks that
happen with preemptive (regular) threads;
</li>
<li>
Reduces the number of callbacks in your code, that sometimes are so
many that you end up with <i>spaghetti code</i>.
</li>
</ul>
<p>The fundamental block used to create tasklets is Python's generators.
Generators are objects that are defined as functions, and when called
produce iterators that return values defined by the body of the function,
specifically <code>yield</code> statements.</p>
<p>The neat thing about generators are not the iterators themselves but
the fact that a function's state is completely frozen and restored
between one call to the iterator's <code>next()</code> and the following
one. This allows the function to return control to a program's main loop
while waiting for an event, such as IO on a socket, thus allowing other
code to run in the mean time. When the specified event occurs, the
function regains control and continues executing as if nothing had
happened.</p>
<h1 class="heading">Structure of a tasklet</h1>
<p>At the outset, a tasklet is simply a python <a target="_top" href="http://www.python.org/peps/pep-0255.html">generator
function</a>, i.e. a function or method containing one or more
<code>yield</code> statements. Tasklets add a couple more requirements
to regular generator functions:</p>
<ol start="1">
<li>
The values contained in <code>yield</code> statements cannot be
arbitrary (see below);
</li>
<li>
After each <code>yield</code> that indicates events, the function <a href="kiwi.tasklet.html#get_event"><code>kiwi.tasklet.get_event</code></a>
must be called to retrieve the event that just occurred.
</li>
</ol>
<h1 class="heading">Syntax for yield in tasklets</h1>
<p>Inside tasklet functions, <code>yield</code> statements are used to
suspend execution of the tasklet while waiting for certain events. Valid
<code>yield</code> values are:</p>
<ul>
<li>
A single <a href="kiwi.tasklet.Message.html"><code>Message</code></a>
object, with a correctly set <i>dest</i> parameter. With this form,
a message is sent to the indicated tasklet. When <code>yield</code>
returns, no event is generated, so the tasklet should <b>not</b> call
<a href="kiwi.tasklet.html#get_event"><code>get_event</code></a>.
</li>
<li>
One, or a sequence of:
<ul>
<li>
<a href="kiwi.tasklet.WaitCondition.html"><code>WaitCondition</code></a>,
meaning to wait for that specific condition
</li>
<li>
<a href="kiwi.tasklet.Tasklet.html"><code>Tasklet</code></a>,
with the same meaning as <a href="kiwi.tasklet.WaitForTasklet.html"><code>WaitForTasklet</code></a><code>(tasklet)</code>
</li>
<li>
generator, with the same meaning as <a href="kiwi.tasklet.WaitForTasklet.html"><code>WaitForTasklet</code></a><code>(Tasklet(gen))</code>
</li>
</ul>
<p>In this case, the tasklet is suspended until either one of the
indicated events occurs. The tasklet must call <a href="kiwi.tasklet.html#get_event"><code>get_event</code></a> in this
case.</p>
</li>
</ul>
<h1 class="heading">Launching a tasklet</h1>
<p>To start a tasklet, the <a href="kiwi.tasklet.Tasklet.html"><code>Tasklet</code></a> constructor
must be used:</p>
<pre class="literalblock">
from kiwi import tasklet
def my_task(x):
[...]
tasklet.Tasklet(my_task(x=0))
</pre>
<p>Alternatively, <a href="kiwi.tasklet.html#run"><code>kiwi.tasklet.run</code></a> can be
used to the same effect:</p>
<pre class="literalblock">
from kiwi import tasklet
tasklet.run(my_task(x=0))
</pre>
<p>Yet another approach is to use the @tasklet.task decorator:</p>
<pre class="literalblock">
from kiwi import tasklet
@tasklet.task
def my_task(x):
[...]
raise StopIteration("return value")
yield my_task(x=0)
retval = tasklet.get_event().retval
</pre>
<h1 class="heading">Examples</h1>
<h2 class="heading">Background timeout task</h2>
<p>This example demonstrates basic tasklet structure and timeout
events:</p>
<pre class="literalblock">
import gobject
from kiwi import tasklet
mainloop = gobject.MainLoop()
def simple_counter(numbers):
timeout = tasklet.WaitForTimeout(1000)
for x in xrange(numbers):
print x
yield timeout
tasklet.get_event()
mainloop.quit()
tasklet.run(simple_counter(10))
mainloop.run()
</pre>
<h2 class="heading">Message passing</h2>
<p>This example extends the previous one and demonstrates message
passing:</p>
<pre class="literalblock">
import gobject
from kiwi import tasklet
mainloop = gobject.MainLoop()
@tasklet.task
def printer():
msgwait = tasklet.WaitForMessages(accept=("quit", "print"))
while True:
yield msgwait
msg = tasklet.get_event()
if msg.name == "quit":
return
assert msg.name == 'print'
print ">>> ", msg.value
@tasklet.task
def simple_counter(numbers, task):
timeout = tasklet.WaitForTimeout(1000)
for x in xrange(numbers):
yield tasklet.Message('print', dest=task, value=x)
yield timeout
tasklet.get_event()
yield tasklet.Message('quit', dest=task)
mainloop.quit()
task = printer()
simple_counter(10, task)
mainloop.run()
</pre><table class="fieldTable"></table></div>
</div>
<div id="splitTables">
<table class="children sortable" id="id19">
<tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.task.html" data-type="Class" class="code">task</a></td>
<td><span>A decorator that modifies a tasklet function to avoid the need to call
<code>tasklet.run(func())</code> or
<code>tasklet.Tasklet(func())</code>.</span></td>
</tr><tr class="function">
<td>Function</td>
<td><a href="kiwi.tasklet.html#get_event" data-type="Function" class="code">get_event</a></td>
<td><span>Return the last event that caused the current tasklet to regain
control.</span></td>
</tr><tr class="function">
<td>Function</td>
<td><a href="kiwi.tasklet.html#run" data-type="Function" class="code">run</a></td>
<td><span>Start running a generator as a <a href="kiwi.tasklet.Tasklet.html"><code>Tasklet</code></a>.</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitCondition.html" data-type="Class" class="code">WaitCondition</a></td>
<td><span>Base class for all wait-able condition objects.</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForCall.html" data-type="Class" class="code">WaitForCall</a></td>
<td><span>An object that waits until it is called.</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForIO.html" data-type="Class" class="code">WaitForIO</a></td>
<td><span>An object that waits for IO conditions on sockets or file
descriptors.</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForTimeout.html" data-type="Class" class="code">WaitForTimeout</a></td>
<td><span>An object that waits for a specified ammount of time (a timeout)</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForIdle.html" data-type="Class" class="code">WaitForIdle</a></td>
<td><span>An object that waits for the main loop to become idle</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForTasklet.html" data-type="Class" class="code">WaitForTasklet</a></td>
<td><span>An object that waits for a tasklet to complete</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForSignal.html" data-type="Class" class="code">WaitForSignal</a></td>
<td><span>An object that waits for a signal emission</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForProcess.html" data-type="Class" class="code">WaitForProcess</a></td>
<td><span>An object that waits for a process to end</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.Message.html" data-type="Class" class="code">Message</a></td>
<td><span>A message that can be received by or sent to a tasklet.</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.WaitForMessages.html" data-type="Class" class="code">WaitForMessages</a></td>
<td><span>An object that waits for messages to arrive</span></td>
</tr><tr class="class">
<td>Class</td>
<td><a href="kiwi.tasklet.Tasklet.html" data-type="Class" class="code">Tasklet</a></td>
<td><span>An object that launches and manages a tasklet.</span></td>
</tr><tr class="function private">
<td>Function</td>
<td><a href="kiwi.tasklet.html#_normalize_list_argument" data-type="Function" class="code">_normalize_list_argument</a></td>
<td><span>returns a list of strings from an argument that can be either list of
strings, None (returns []), or a single string returns ([arg])</span></td>
</tr>
</table>
</div>
<div id="childList">
<div class="basefunction">
<a name="kiwi.tasklet.get_event">
</a>
<a name="get_event">
</a>
<div class="functionHeader">
def
get_event():
</div>
<div class="docstring functionBody">
<div><p>Return the last event that caused the current tasklet to regain
control.</p><table class="fieldTable"><tr class="fieldStart"><td class="fieldName">Note</td><td colspan="2">this function should be called exactly once after each yield that includes
a wait condition.</td></tr></table></div>
</div>
</div><div class="basefunction">
<a name="kiwi.tasklet.run">
</a>
<a name="run">
</a>
<div class="functionHeader">
def
run(gen):
</div>
<div class="docstring functionBody">
<div><p>Start running a generator as a <a href="kiwi.tasklet.Tasklet.html"><code>Tasklet</code></a>.</p><table class="fieldTable"><tr class="fieldStart"><td class="fieldName">Parameters</td><td class="fieldArg">gen</td><td>generator object that implements the tasklet body.</td></tr><tr class="fieldStart"><td class="fieldName">Returns</td><td colspan="2">a new <a href="kiwi.tasklet.Tasklet.html"><code>Tasklet</code></a>
instance, already running.</td></tr><tr class="fieldStart"><td class="fieldName">Note</td><td colspan="2">this is strictly equivalent to calling <code>Tasklet(gen)</code>.</td></tr></table></div>
</div>
</div><div class="basefunction private">
<a name="kiwi.tasklet._normalize_list_argument">
</a>
<a name="_normalize_list_argument">
</a>
<div class="functionHeader">
def
_normalize_list_argument(arg, name):
</div>
<div class="docstring functionBody">
<div><p>returns a list of strings from an argument that can be either list of
strings, None (returns []), or a single string returns ([arg])</p><table class="fieldTable"></table></div>
</div>
</div>
</div>
<address>
<a href="index.html">API Documentation</a> for Kiwi, generated by <a href="https://github.com/twisted/pydoctor/">pydoctor</a> at 2015-12-15 11:23:22.
</address>
</div>
<script type="text/javascript" src="pydoctor.js"></script>
</body>
</html>
|