EzDevInfo.com

goless

Go-like semantics built on top of Stackless Python.

Understand goless.select from the sample code

I have discovered python implementation of Goroutines, https://goless.readthedocs.org/en/latest/ and having a play

Given the following code from the documentation:

c1 = goless.chan()
c2 = goless.chan()

def func1():
    time.sleep(1)
    c1.send('one')
goless.go(func1)

def func2():
    time.sleep(2)
    c2.send('two')
goless.go(func2)

for i in range(2):
    case, val = goless.select([goless.rcase(c1), goless.rcase(c2)])
    print(val)

And it prints:

one
two

Documentation about the select method

Select the first case that becomes ready. If a default case (goless.dcase) is present, return that if no other cases are ready. If there is no default case and no case is ready, block until one becomes ready.

So I went ahead and change the sleep(1) to sleep(3) as below:

c1 = goless.chan()
c2 = goless.chan()

def func1():
    time.sleep(3)
    c1.send('one')
goless.go(func1)

def func2():
    time.sleep(2)
    c2.send('two')
goless.go(func2)

for i in range(2):
    case, val = goless.select([goless.rcase(c1), goless.rcase(c2)])
    print(val)

And I thought it would print:

two
one

But it printed:

one
two

Why is that?


Source: (StackOverflow)