[Stackless] Select() on channels?

Arnar Birgisson arnarbi at gmail.com
Sat Sep 15 02:56:16 CEST 2007


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.

>     tasklets = []
>     for inputChannel in channels:
>         tasklets.append(stackless.tasklet(alt_receiver)(resultChannel,
> inputChannel))
>     firstTasklet, firstChannel, result = resultChannel.receive()
>     for tasklet in tasklets:
>         if tasklet is not firstTasklet:
>             tasklet.kill()
>     return firstChannel, result

Arnar




More information about the Stackless mailing list