草庐IT

go - 混淆 channel 控制流

coder 2024-07-10 原文

我正在尝试编写一个事件监听器并尝试控制监听器内部的状态流。 我知道我错过了 channel 使用的一些原则,代码可能看起来很愚蠢。但是,如果有人可以帮助我了解我的错误是什么以及如何改进它,我将不胜感激。

此代码无法运行:

package main

import (
    "fmt"
    "time"
)

type A struct {
    count int
    ch    chan bool
    exit  chan bool
}

func (this *A) Run() {
    for {
        select {
        case <-this.ch:
            this.handler()
        case <-this.exit:
            return
        default:
            time.Sleep(20 * time.Millisecond)
        }
    }
}

func (this *A) handler() {
    println("hit me")
    if this.count > 2 {
        this.exit <- true
    }
    fmt.Println(this.count)
    this.count += 1
}

func (this *A) Hit() {
    this.ch <- true
}

func main() {
    a := &A{}
    a.ch = make(chan bool)
    a.exit = make(chan bool)

    go a.Hit()
    go a.Hit()
    go a.Hit()
    go a.Hit()
    a.Run()

    fmt.Println("s")
}

它引发错误:

hit me
0 
hit me
1
hit me
2
hit me
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:
main.(*A).handler(0x2101bf000)
/Users/yeer/go/src/github.com/athom/practice/channel-controll.go:31 +0x60
main.(*A).Run(0x2101bf000)
/Users/yeer/go/src/github.com/athom/practice/channel-controll.go:19 +0x66
main.main()
/Users/yeer/go/src/github.com/athom/practice/channel-controll.go:50 +0xed
exit status 2

但是,这段代码有效:

package main

import (
    "fmt"
    "time"
)

type A struct {
    count int
    ch    chan bool
    exit  chan bool
}

func (this *A) Run() {
    for {
        select {
        case <-this.ch:
            this.handler()
        case <-this.exit:
            return
        default:
            time.Sleep(20 * time.Millisecond)
        }
    }
}

func (this *A) handler() {
    println("hit me")
}

func (this *A) Hit() {
    this.ch <- true
    if this.count > 2 {
        this.exit <- true
    }
    fmt.Println(this.count)
    this.count += 1
}

func main() {
    a := &A{}
    a.ch = make(chan bool)
    a.exit = make(chan bool)

    go a.Hit()
    go a.Hit()
    go a.Hit()
    go a.Hit()
    a.Run()

    fmt.Println("s")
}

为什么不能在同级 channel 处理程序中触发另一个 channel ?

最佳答案

您的代码死锁是因为当您在退出 channel 上发送时 this.exit <- true这是来自您从该 channel 接收到的同一个 goroutine,并且永远无法完成。

可能最明智的做法是用 bool 标志替换退出 channel 。如果你这样做,那么它工作正常。

Play

type A struct {
    count int
    ch    chan bool
    exit  bool
}

func (this *A) Run() {
    for !this.exit {
        select {
        case <-this.ch:
            this.handler()
        default:
            time.Sleep(20 * time.Millisecond)
        }
    }
}

func (this *A) handler() {
    println("hit me")
    if this.count > 2 {
        this.exit = true
    }
    fmt.Println(this.count)
    this.count += 1
}

func (this *A) Hit() {
    this.ch <- true
}

func main() {
    a := &A{}
    a.ch = make(chan bool)

    go a.Hit()
    go a.Hit()
    go a.Hit()
    go a.Hit()
    a.Run()

    fmt.Println("Done")
}

另一种选择是使用 go this.handler() 在自己的 go 例程中运行每个处理程序

Play

func (this *A) Run() {
    for {
        select {
        case <-this.ch:
            go this.handler() // add go here
        case <-this.exit:
            return
        default:
            time.Sleep(20 * time.Millisecond)
        }
    }
}

最后你可以缓冲退出 channel

Play

func main() {
    a := &A{}
    a.ch = make(chan bool)
    a.exit = make(chan bool, 5) // add buffer here

    go a.Hit()
    go a.Hit()
    go a.Hit()
    go a.Hit()
    a.Run()

    fmt.Println("Done")
}

关于go - 混淆 channel 控制流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20027901/

有关go - 混淆 channel 控制流的更多相关文章

  1. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  2. ruby-on-rails - 带 Spring 锁的 Rails 4 控制台 - 2

    我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.

  3. ruby-on-rails - openshift 上的 rails 控制台 - 2

    我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新ruby​​gems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems

  4. C51单片机——实现用独立按键控制LED亮灭(调用函数篇) - 2

    说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时

  5. ruby-on-rails - 在 Rails 控制台中使用 asset_path - 2

    在我的Character模型中,我添加了:字符.rbbefore_savedoself.profile_picture_url=asset_path('icon.png')end但是,对于数据库中已存在的所有角色,它们的profile_picture_url为nil。因此,我想进入控制台并遍历所有这些并进行设置。在我试过的控制台中:Character.find_eachdo|c|c.profile_picture_url=asset_path('icon.png')end但这给出了错误:NoMethodError:undefinedmethod`asset_path'formain:O

  6. ruby-on-rails - 带有 Pry 的 Rails 控制台 - 2

    当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question

  7. ruby - 将全局 $stdout 重新分配给控制台 - ruby - 2

    我正在尝试将$stdout设置为临时写入一个文件,然后返回到一个文件。test.rb:old_stdout=$stdout$stdout.reopen("mytestfile.out",'w+')puts"thisgoesinmytestfile"$stdout=old_stdoutputs"thisshouldbeontheconsole"$stdout.reopen("mytestfile1.out",'w+')puts"thisgoesinmytestfile1:"$stdout=old_stdoutputs"thisshouldbebackontheconsole"这是输出。r

  8. ruby-on-rails - Ruby 流量控制 : throw an exception, 返回 nil 还是让它失败? - 2

    我在思考流量控制的最佳实践。我应该走哪条路?1)不要检查任何东西并让程序失败(更清晰的代码,自然的错误消息):defself.fetch(feed_id)feed=Feed.find(feed_id)feed.fetchend2)通过返回nil静默失败(但是,“CleanCode”说,你永远不应该返回null):defself.fetch(feed_id)returnunlessfeed_idfeed=Feed.find(feed_id)returnunlessfeedfeed.fetchend3)抛出异常(因为不按id查找feed是异常的):defself.fetch(feed_id

  9. ruby-on-rails - ruby 新手,有人可以帮我从控制台破译这个错误吗? - 2

    我真的只是不确定这意味着什么或我应该做什么才能让网页在我的本地主机上运行。现在它只是显示一个错误,上面写着“我们很抱歉,但出了点问题。”当我运行railsserver并在chrome中打开localhost:3000时。这是控制台输出:StartedGET"/users/sign_in"for127.0.0.1at2013-07-0512:07:07-0400ProcessingbyDevise::SessionsController#newasHTMLCompleted500InternalServerErrorin55msNoMethodError(undefinedmethod`

  10. ruby-on-rails - Rails 控制台的 YAML 输出 - 2

    在Rails控制台中执行类似yGrau.all的命令时,我得到这些奇怪的!binary字符串而不是属性名称。知道如何解决这个问题吗?谢谢。irb(main):003:0>yGrau.all←[1m←[36mGrauLoad(0.0ms)←[0m←[1mSELECT"graus".*FROM"gr←[1m←[35mEXPLAIN(0.0ms)←[0mEXPLAINQUERYPLANSELECT"grauEXPLAINfor:SELECT"graus".*FROM"graus"0|0|0|SCANTABLEgraus(~1000000rows)----!ruby/object:Grauat

随机推荐