[Stackless] Figuring out stackless for use in Twisted

Christian Tismer tismer at tismer.com
Tue Jul 29 19:05:49 CEST 2003


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.

[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.

...

> 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.

> 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.

> 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()

...

> Ok. I just need to force it to switch to main, somehow. :-)

Right.

>>>Just an annoying little thing. I've just been reading the docstrings
>>>by browsing the source (and help(stackless.tasklet) still works). :-)
>>
>>Oops, yes this is right. Since a while, I'm storing
>>special values in the dict of the frame type, under
>>an integer key. Didn't realize this.
>>So probably I have to change this? Is it "forbidden"
>>to have non-string keys in type dicts?
> 
> 
> Well, I would say "it is forbidden to do anything that will break
> getattr(o, key)" :-)

Oh, yes this is correct.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at tismer.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  pager +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/

_______________________________________________
Stackless mailing list
Stackless at www.tismer.com
http://www.tismer.com/mailman/listinfo/stackless




More information about the Stackless mailing list