[Stackless] Re: [Python-Dev] uthread strawman

Christian Tismer tismer at tismer.com
Thu Nov 9 14:04:00 CET 2000


Christian Tismer wrote:

I wrote:
> There is one application of continuations which I still consider
> worthy. I'm shure that many people find it incredibly ugly.
> Using continuations, I can build method-like functions
> without classes and instances, which perform incredibly
> fast. This cannot be done with simple one-shot continuations;
> of course a class method would do the same, but slower:
> 
> <script language="python">
> function expensive_to_init_and_cheap_to_call(*args):
>     pass # initialize many local variables
>     param = continuation.return_current() # ***
>     return do_cheap_calculation(param)
> 
> # generate and initialize such a function
> 
> fastfunc = expensive_to_init_and_cheap_to_call(manyargs)

But this is again no argument for continuations, since
generators provide an equally performant solution:
(assuming a generator implementation, which passes the
generator object as first argument to itself. The
initialization part ends at self.start(), which
leaves the generator frame as is, and acts as an
entry point.)

function easily_done_with_generators(self, *args):
    pass # initialize many local variables
    param = self.start()
    while 1:
        result = do_cheap_calculation(param)
        param = self.suspend(result)

fastfunc = generator(easily_done_with_generators, manyargs)

fastfunc(42) # ...

----------------------

This implementation works without continuations and gives
equally fast generators.

Note the self.start() method: It is equivalent to the
continuation.return_current() function:
Leave the current frame as its own result, and provide
an entry point which accepts a value.

self.suspend() returns from the frame without ending it.
It returns a value, but preserves the entry point.

Does anybody know of a useful example where continuations
are really needed? The only remaining idea is at the moment
modelling of a case construct, but this reason is easily
removed by adding a switch statement to Python. :-)

cheers - chris   (stackless and continuationless)

-- 
Christian Tismer             :^)   <mailto:tismer at tismer.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com
_______________________________________________
Stackless mailing list
Stackless at starship.python.net
http://starship.python.net/mailman/listinfo/stackless



More information about the Stackless mailing list