Continuations and threads (was Re: Iterators & generators)

Aahz Maruch aahz at netcom.com
Thu Feb 17 23:54:43 CET 2000


In article <38AC5FCA.75F4EA42 at prescod.net>,
Paul Prescod  <paul at prescod.net> wrote:
>
>I'm not sure if I believe what Tim said about implementing threads with
>continuations or vice versa. A continuation is, in my mind, only vaguely
>related to a thread.

Assuming that I've understood everything you've said about
continuations, I think I also understand what Tim meant about modeling
threads with continuations and vice-versa.  Seems to me that one could
easily implement continuations with a process that forks off and blocks
waiting for one of two signals: resume or die.  (When a process forks,
both processes have almost exactly the same state, right?)

If everything I've said above is correct, then it should be reasonably
straightforward to also model continuations with threads (but probably
not Threading.py).  I leave the reverse model as an exercise for the
reader.  ;-)

>def producer():
>    global producer_continuation, consumer_continuation, data
>    while 1:
>	producer_continuation=currentContinuation()
>	data = readSomeData()
>	consumer_continuation( )
>
>def consumer():
>   global producer_continuation, consumer_continuation, data
>   while 1:
>	consumer_continuation=currentContinuation()
>	producer_continuation( )
>	print data
>
>producer_continuation=None
>consumer_continuation=None
>data=None

To demonstrate what I think is my understanding, let me rewrite your
code a bit for correctness and simplicity:

def producer():
    global producer_continuation, consumer_continuation, data
    producer_continuation=currentContinuation()
    while 1:
        data = readSomeData()
        consumer_continuation()

def consumer():
    global producer_continuation, consumer_continuation, data
    consumer_continuation=currentContinuation()
    producer()
    while 1:
        print data
        producer_continuation()

producer_continuation=None
consumer_continuation=None
data=None
consumer()

Am I right?
--
                      --- Aahz (Copyright 2000 by aahz at netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Our society has become so fractured that the pendulum is swinging
several different directions at the same time



More information about the Python-list mailing list