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

Guillaume Chereau charlie137 at gmail.com
Wed Jan 21 11:45:55 CET 2009

Just realized i clicked "reply" instead of 'reply to all"

---------- Forwarded message ----------
From: Guillaume Chereau <charlie137 at gmail.com>
Date: Wed, Jan 21, 2009 at 12:00 PM
Subject: Re: [Python-ideas] standard library proposal for "tasklets"
using  generators
To: Guido van Rossum <guido at python.org>

Thanks for the answer,

I understand the concern here.

I still think having such a library, if not as a standard python lib,
but at least as an independent one, that could then be used by all the
project using such pattern (twisted, kiwi, gobject, etc.) would be a
good idea.
I will try to contact those different projects for suggestions.

I also have to say one of my secret hope was that the pypy interpreter
could eventually understand this kind of 'tasklet' generators and
generate C or other language code out of it, since pypy already has
support for tasklets, but using a syntax that can't be interpreted by

- Guillaume

On Wed, Jan 21, 2009 at 10:56 AM, Guido van Rossum <guido at python.org> wrote:
> 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