[Stackless] killing tasklets

Kristján Valur Jónsson kristjan at ccpgames.com
Fri Nov 23 12:34:23 CET 2007



> -----Original Message-----
> We can have another flag, probably a computed one,
> if it is useful.
>
> But simply ignoring this and killing right away should
> be fine in this case.

Well, the thing is, we expect all tasklets to run to their end, with something like:

def Wrap():
        try:
                tasklet_func()
        finally:
                signal_that_tasklet_is_done(self)

And then let the tasklet run the Wrap() function.
All tasklets are guaranteed to reach the finally clause, except tasklets that are killed before they get the chance to be run in the first place.  So, we need to have a special case to handle them.  Here is how one could do this:

def Kill(tasklet):
        try:
                tasklet.run()  #make sure it runs
        except:
                pass
        tasklet.kill()

def Kill(t):
        if tasklet_hasnt_run(t):
                t.remove()
                signal_that_tasklet_is_done(t)
        else:
                t.kill()

All are a bit nasty because they mean that we can't just t.kill(),  we suddenly have coupling to the tasklet bookkeeping at the site where we do the killing.

We could also make sure that a fresh tasklet is always t.run() thus avoiding the issue, but that would constrain us too much.  Sometimes we want to construct a tasklet for running later.

Ok, My final idea is to add a "started" attribute to the tasklet subclass which is set to True in the Wrapper function.  We can then define an overriding kill() method which tests for this...  That is perhaps the simples.

K




More information about the Stackless mailing list