python - Understand goless.select from the sample code -


i have discovered python implementation of goroutines, https://goless.readthedocs.org/en/latest/ , having play

given following code 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)  in range(2):     case, val = goless.select([goless.rcase(c1), goless.rcase(c2)])     print(val) 

and prints:

one 2 

documentation select method

select first case becomes ready. if default case (goless.dcase) present, return if no other cases ready. if there no default case , no case ready, block until 1 becomes ready.

so went ahead , change sleep(1) sleep(3) 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)  in range(2):     case, val = goless.select([goless.rcase(c1), goless.rcase(c2)])     print(val) 

and thought print:

two 1 

but printed:

one 2 

why that?

since there no answer went have dig on project repo, found similar question here:

https://github.com/rgalanakis/goless/issues/42

most notable:

using time.sleep pauses current thread. not switching between coroutines. have use gevent.sleep or similar mechanism stackless (or goless.backend.yield).

so seemed misunderstood goless create different threads wrong.


Comments