[Stackless] Figuring out stackless for use in Twisted

Christopher Armstrong radix at twistedmatrix.com
Wed Jul 30 04:49:50 CEST 2003


On Tue, Jul 29, 2003 at 07:05:49PM +0200, Christian Tismer wrote:
> Christopher Armstrong wrote:
> 
> >On Mon, Jul 28, 2003 at 01:51:08PM +0200, Christian Tismer wrote:
> >
> >>Christopher Armstrong wrote:
> ...
> 
> >Can't you just put up a blank wiki on a web server and let the
> >community build it? I would certainly contribute. :-)
> 
> Yeah, I'm just so very busy at the moment,
> preparing several projects, travelling,
> that I don't want to bad promises again.

Hmm. setting up a MoinMoin is easy. I can do this for you. Although it
may be just as much of a hassle to give me access to whatever server
you want to run it on as setting up the MoinMoin itself :) Or, I might
be able to set one up on a server I already have access to. If you
want me to see about doing this, I will.

> [become]
> 
> >Hmm. So that means I need to then call the run method on the new
> >tasklet? or maybe __call__?  hrmm, the code I've been reading as an
> >example (Sheared -- thanks Sune!) doesn't seem to do that. So I still
> >don't get it. It just passes a channel object to the `become' method,
> >and doesn't care about its return value. Maybe I should get Sune to
> >explain things more :-)
> 
> Well. t.become is some tricky stuff for lazy guys.
> The recommended, "clean" way to do it is to
> create a tasklet around a function, to parameterize
> it, and that's it.
> If you also want to force it to run right now, you
> can call it's run() method.

Whew, this looks a lot saner than become() :)

> ...
> 
> >I want to control all context swithes myself. I think that's what
> >`setatomic' allows? But anyway, my reactor, every time it calls
> >"user-code", is going to start up a new tasklet for that code, and
> >call it (there may be future optimizations that won't start a new
> >tasklet for _every_ user-function, but I do not want to think about
> >that yet). 
> 
> I see. What you want is to write your won scheduler.
> This is something that I want to support in Stackless,
> but never really finished. It is doable, anyway.
> 
> setatomic? This is a flag that prevends Stackless
> from doing pre-emptive auto-scheduling. But even
> that is not yet implemented.

Hrm... Does that mean that I might get context switches in arbitrary
points in my code, or only when "blocking" on a channel.send() or
channel.receive()? If the latter, I would call that "implicit
cooperative scheduling", not really pre-emptive. :-) I hope it's the
latter!


> >The user code itself needs to say when it needs to block (I will
> >provide nice abstractions for this) and I _always_ want the reactor
> >(i.e., main tasklet) to be switched to when the user-tasklet
> >blocks. The reactor will later say when to continue the user-tasklet,
> >when the thing that the user-tasklet was waiting for has occured.
> 
> Ok, so you would just make sure that there are only
> two tasklets active: main and the other one.
> You can either grab all tasklets by using the remove()
> method, but it would be much nicer and more in the
> sense of it to use channels for that.

Ok, I'll look into this. thank you.

> >So this is what I'm thinking it should look like
> >
> >def callUserFunction(self, f, *args, **kwargs):
> >    t = stackless.tasklet()
> >    c = stackless.channel()
> >    # whatever other setup code
> >    remember the tasklet and channel for later reference
> >    call the function `f' in tasklet `t', making sure it knows about
> >         channel `c'
> 
> Ok, if we assume that f has a channel parameter,
> then we get something like this:
> 
> def f(chan):
>     # something
>     if want_to_block:
>         stuff = chan.receive()
> 
> def callUserFunction(self, f, *args, **kwargs):
>     t = stackless.tasklet(f)
>     c = stackless.channel()
>     t(c, *args, **kwargs)
>     t.run()


I really needed this example! Thanks. Hmm, __call__ needs a docstring
:)

Thanks a lot for your help! Hopefully I have enough information now to
get something working. If not, maybe I'll bug Sune some more :-)

--
 Twisted | Christopher Armstrong: International Man of Twistery
  Radix  |          Release Manager,  Twisted Project
---------+     http://twistedmatrix.com/users/radix.twistd/




More information about the Stackless mailing list