[Stackless] Select() on channels?

Santiago Gala sgala at apache.org
Sat Sep 15 06:09:53 CEST 2007


El sáb, 15-09-2007 a las 00:56 +0000, Arnar Birgisson escribió:
> Hi folks,
> 
> On 9/14/07, Richard Tew <richard.m.tew at gmail.com> wrote:
> ...
> > def alt_select(*channels):
> >     resultChannel = stackless.channel()
> >     # See if anything is already waiting, and if so return it.
> >     for c in channels:
> >         if c.balance > 0:
> >             return c.receive()
> 
> If the alt_select is inside a loop and there's always something
> available on some channel, this will cause starvation of the channels
> that come after that one in the list. The CSP model calls for
> non-determinism here but with a condition that when multiple channels
> have available data, you should give them all an equal probability of
> being selected so in the long run they all get served.
> 

A simple way to solve it would be to turn alt_select into a generator
(possibly wrapped in a "while True:") and call it successively. Changing
return into yield will keep just enough state to round-robin all
channels, but either the API changes to return a generator or the state
needs to be kept somewhere, by storing the generator and calling next()
on it for every call to alt_select...

For a truly stateless implementation alt_select should probably use
random "shuffling" of the list of channels before iterating. 

Regards
----
Santiago Gala
http://memojo.com/~sgala/blog/







More information about the Stackless mailing list