[Python-ideas] standard library proposal for "tasklets" using generators

Guillaume Chereau charlie137 at gmail.com
Wed Jan 21 03:36:05 CET 2009

Thanks to the introduction of new generator features, as described in
PEP 0342, it is possible to write what I would call "tasklet" (I am no
sure what should be the term exactly).
tasklet are a way to write callback based code that looks like thread. Example :

(assuming we have a function sleep(sec, callback) that will call the
callback after `sec` seconds)

    print "sleep"
    yield Wait(sleep, 5)
    print "called after sleep"
    yield 10 # returned value

    for i in range(10):
        value = yield task1()

(the same thing using callback only would be quite difficult to read)

The library should provide -the names are not very important- a
Tasklet class, plus a tasklet decorator to turn any generator into a
tasklet, plus a Wait function to turn a callback function into a
tasklet, plus maybe a few other functions.

In many cases this kind of library would be very useful. I am
currently using something very similar for a project I am working on
[0]. the kiwi project also have a similar library [1] (but the syntax
is not really nice because they don't take advantage of the PEP 0342

I guess more and more people may try to use similar library in the
future, but the problem is that the implementation of it is really not
trivial, so I would like to suggest having such a library in the
standard set of python libraries.

Do you think this is a good idea ?



[0] http://git.openmoko.org/?p=tichy.git;a=blob;f=tichy/tasklet.py
[1] http://www.async.com.br/projects/kiwi/api/kiwi.tasklet.html


More information about the Python-ideas mailing list