[Stackless] Select() on channels?

Arnar Birgisson arnarbi at gmail.com
Sat Sep 15 12:51:28 CEST 2007


On 9/15/07, Santiago Gala <sgala at apache.org> wrote:
> > 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()

Forgot to mention in the first place that this should of course be
return c, c.receive()

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

Arnar




More information about the Stackless mailing list