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

Guido van Rossum guido at python.org
Wed Jan 21 03:56:48 CET 2009


The pattern of invoking all sorts of stuff that may wait indefinitely
using yield has been proposed before, but I find it creates rather
awkward code compared to using plain threads. Not something I'd like
to encourage by having standard decorators and other APIs around this
idea. I believe Twisted has something like this, and that's probably a
good place to isolate such patterns; in their case they really
want/need this, so they put up with the awkwardness. Putting the
necessary decorators/APIs in the language doesn't do much about the
awkwardness, so I don't think we should do this.

On Tue, Jan 20, 2009 at 6:36 PM, Guillaume Chereau <charlie137 at gmail.com> wrote:
> Hello,
> 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)
>
> @tasklet
> task1():
>    print "sleep"
>    yield Wait(sleep, 5)
>    print "called after sleep"
>    yield 10 # returned value
>
> @tasklet
> task2():
>    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
> features.)
>
> 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 ?
>
> cheers,
>
> Guillaume
>
> [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
>
> --
> http://charlie137.blogspot.com/
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list