[Stackless] Modelling simulated time and granular interruptions
stackless at kaishaku.org
stackless at kaishaku.org
Tue Jul 4 02:10:00 CEST 2006
> I am writing a non-realtime proof-of-concept simulation.
>
> I have Actors. They perform actions which take a specific
> amount of time. The trouble is, Actors can be interrupted
> while performing an action and could cancel that action.
This example should show what I'm trying to do.
The thing is, actors will perform as much as they
can in a specified time (number of ticks). Actions
will occur across tick boundaries. Actions can be
cancelled and interrupted, to be resumed later.
My example seems to do what I want, but I find myself
constantly wondering what task will execute next and
sometimes what task *should* execute next.
I feel this will only get worse and imagine I must
be misusing stackless. What can I do better here?
import stackless
class Actor:
def __init__(self):
self.name = None
self.queue = []
self.acting = True
self.ticker = stackless.channel()
self.tick = 0
def live(self):
while self.acting:
if len(self.queue)>0:
self.performact(self.queue.pop(0))
def performact(self,action):
print 'performact', action
fn = action[0]
fn(*action[1:])
def say(self,msg):
i = 0
while i!=len(msg):
print msg[i],
i += 1
self.usetick()
if self.state.cancel==True:
self.state = State()
break
if self.state.interrupt==True:
print '[interrupted say]'
self.state = State()
self.queue.append( (self.say,msg) )
#stackless.schedule(self.say)(msg)
break
print ''
def walk(self,distance):
i = 0
while i!=distance:
print '#',
i += 1
self.usetick()
if self.state.cancel==True:
self.state = State()
break
if self.state.interrupt==True:
print '[interrupted walk]'
self.state = State()
#self.queue.append( (self.walk,distance) )
stackless.schedule(self.walk)(distance-i)
break
print ''
def usetick(self):
self.tick += 1
self.state = self.ticker.receive()
class State:
def __init__(self,cancel=False,interrupt=False):
self.cancel = cancel
self.interrupt = interrupt
# send ticker
def chronos(actors):
while 1:
for actor in actors:
actor.ticker.send(State())
stackless.schedule()
def test(a):
print '...interrupting...'
a.ticker.send(State(interrupt=True))
def main():
a = Actor()
a.name = 'John'
a.queue = [
(a.say,"Where is everybody?"),
(a.walk,10)
]
stackless.tasklet(a.live)()
stackless.tasklet(test)(a)
stackless.tasklet(test)(a)
stackless.tasklet(test)(a)
stackless.tasklet(chronos)([a])
stackless.run()
if __name__=='__main__':
main()
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless
More information about the Stackless
mailing list