Using continuations to do your own scheduling...

Joe Kraska jkraska at bbn.com
Thu May 4 02:43:44 CEST 2000


Christian, et al.,

Thought you might appreciate this. It's a fairly simple
example of how to do your own scheduling with continuations
in Python. In a more advanced M&S setting, the call to
time.sleep(s.ticks) would instead be something which
coordinated the passage of model time, not actual wall-
clock time.

Note the presumptions of call depth and the scheduler;
this obviously has to be thought out carefully and could
get you into major trouble if you're wrong. The trick
is to make certain that no one but you is calling your
procedures.

Joe.
----

import continuation
import time

class Schedulable:
    c=0
    ticks=0

def fail():
    c=continuation.caller(2)
    c.jump(0)

def succeed():
    c=continuation.caller(2)
    c.jump(1)

def passtime(ticks):
    c1=continuation.caller(1)
    c2=continuation.caller(2)
    s=Schedulable()
    s.c = c1
    s.ticks = ticks
    c2.jump(s)

def procedure1():
    passtime(1)
    succeed()

def procedure2():
    passtime(1)
    fail()

def procedure3():
    passtime(1)

def dispatch(f):
    s=f()
    t = type(s)
    if t.__name__ == "instance": #WRONG: what's the syntax f/ "Schedulable"?
        time.sleep(s.ticks)
        s.c.jump()
    elif s == 1:
        print "    procedure succeeded explicitly"
    elif s == 0:
        print "    procedure failed explicitly"
    else:
        print "    WARNING UNINTERPRETABLE VALUE: IMPLICIT SUCCESS"

def test():
    dispatch(procedure1)
    dispatch(procedure2)
    dispatch(procedure3)

test()







More information about the Python-list mailing list