a simple example of Stackless Python

brucemdawson at my-deja.com brucemdawson at my-deja.com
Wed Nov 1 20:23:09 EST 2000


In article <V7oL5.37775$f01.44754 at skycache.prestige.net>,
  "Evan Simpson" <evan at 4-am.com> wrote:

...
> I'll give it a shot. "this = continuation.current()" creates a
continuation
> object, and "this.update(n)" does two things: First, it updates the
> continuation object so that calling it will cause program execution to
> continue at that line, as though "this.update(n)" had just returned
the
> value passed in the call.  Second, it evaluates to n, so that the line
acts
> as though it were "k = n".
>
> Thus, "this(k-1)"  jumps to the "k = this.update(n)" line and makes it
act
> as though it were "k = k-1".  It means "continue at the point just
after we
> were updated, with k-1 on top of the stack".
>
> The whole function behaves exactly like:
>
> k = n
> while k:
>   k = k-1
>
> ...and demonstrates how a continuation can be used to construct a
simple
> loop.
>
> --
> Cheers,
>
> Evan @ digicool & 4-am

Okay, that helps. Now, my 'plan' for continuations is I want to
create lightweight threads. I want to be able to start a
function as a thread. Based on your explanation, I think this
would work:

def main():
    my_continuation = continuation.current()
    k = my_continuation.update(0)
    if k:
        # Sub thread goes here.
        do_thread_func()
        del my_continuation.link
    else:
        # Main thread goes here, and starts sub-thread.
        my_continuation(1)
    # Main thread continues here.

My intent was to make the behaviour look a bit like
fork() in unix.

Okay, the big flaw in my code is that no thread is
actually created - all I've done is used a peculiar
syntax to call do_thread_func() - but I'm just
starting. Am I making tiny amounts of sense?


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list