Understand goless.select from the sample code

huangapple go评论82阅读模式
英文:

Understand goless.select from the sample code

问题

我发现了Goroutines的Python实现,链接为https://goless.readthedocs.org/en/latest/,并进行了尝试。

给定以下来自文档的代码:

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)

它打印出:

one
two

关于select方法的文档:
> 选择第一个准备好的case。如果存在默认case(goless.dcase),则在没有其他case准备好时返回默认case。如果没有默认case且没有case准备好,则阻塞直到有case准备好。

所以我将sleep(1)更改为sleep(3),如下所示:

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)

我以为它会打印:

two
one

但实际上打印出:

one
two

为什么会这样呢?

英文:

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?

答案1

得分: 0

由于没有答案,我去查看了项目存储库,并在这里找到了一个类似的问题:

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

最值得注意的是:

> 使用time.sleep只会暂停当前线程,它不会在协程之间进行切换。您需要使用gevent.sleep或类似的机制来进行Stackless的切换(或者goless.backend.yield)。

所以看起来我误解了goless会创建不同的线程,但我错了。

英文:

Since there is no answer so I went to have a dig on the project repo, found a similar question here:

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

Most notable:

> using time.sleep just pauses the current thread. It does not do any switching between coroutines. You would have to use gevent.sleep or a similar mechanism for Stackless (or goless.backend.yield).

So it seemed that I misunderstood the goless would create different threads but I was wrong.

huangapple
  • 本文由 发表于 2015年7月17日 09:59:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/31467321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定