[Stackless] Select() on channels?

Tim Kientzle tim at metaweb.com
Mon Sep 17 18:45:56 CEST 2007


Arnar Birgisson wrote:
> On 9/15/07, Santiago Gala <sgala at apache.org> wrote:
> 
>> For a truly stateless implementation alt_select should probably use
>> random "shuffling" of the list of channels before iterating.
> 
> Yes, forgetting about efficiency for a while, something like this:
> 
> import random
> ready_channels = [c for c in channels if c.balance > 0]
> if len(ready_channels):
>     c = random.choice(ready_channels)
>     return c, c.receive()

Why not just move each ready channel to the
end of the list?  That would be both fair
and efficient.  That would require modifying
the API a bit:
    chset = [ch1, ch2]
    while True:
       ...
       alt_select(chset)
instead of
    alt_select(*chset)

Modifying arguments like this is a bit tasteless,
I'll agree.  Alternatively, alt_select() could be
implemented as one method of a "channel set"
object.

Tim Kientzle




More information about the Stackless mailing list