亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

sync.Waitgroup 不被尊重

sync.Waitgroup 不被尊重

Go
絕地無雙 2022-10-10 19:50:25
我注意到許多 goroutines 仍在運行,即使程序應該等待它們全部完成。我的理解是添加一個等待組可以解決這個問題,但它沒有。if len(tf5) > 0 {        SplitUpAndSendEmbedToDiscord(5, tf5)    }    if len(tf15) > 0 {        SplitUpAndSendEmbedToDiscord(15, tf15)    }    if len(tf30) > 0 {        SplitUpAndSendEmbedToDiscord(30, tf30)    }    if len(tf60) > 0 {        SplitUpAndSendEmbedToDiscord(60, tf60)    }}// IntradayStratify - go routine to run during market hoursfunc IntradayStratify(ticker string, c chan request.StratNotification, wg *sync.WaitGroup) {    defer wg.Done()    candles := request.GetIntraday(ticker)    for _, tf := range timeframes {        chunkedCandles := request.DetermineTimeframes(tf, ticker, candles)        if len(chunkedCandles) > 1 {            highLows := request.CalculateIntraDayHighLow(chunkedCandles)            // logrus.Infof("%s Highlows calculated: %d", ticker, len(highLows))            // Should have more than 2 candles to start detecting patterns now            if len(highLows) > 2 {                bl, stratPattern := request.DetermineStratPattern(ticker, tf, highLows)                if bl {                    c <- stratPattern                }            }        }        // otherwise return an empty channel        c <- request.StratNotification{}    }}func main() {  RunIntradayScanner()}for我期望程序在循環遍歷符號后再次變成單線程。相反,stdout 如下所示,看起來 goroutines 仍在返回。結果應該是每行寫著“ Pattern XX found for timeframe ”也將有一個相應的“ Sending to discord ”輸出行。
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

每次啟動 goroutine 后,原始代碼會阻塞,等待通過非緩沖通道發送一個值,此外,當WaitGroup倒計時時通道關閉,這也關閉了接收端的通道。

恕我直言,一般規則是:

不要從接收方關閉通道,如果通道有多個并發發送方,也不要關閉通道。

package main


import (

    "fmt"

    "strings"

)


type StratNotification struct {

    Symbol string

}


func GetSymbols() []StratNotification {

    return []StratNotification{

        {Symbol: "a"},

        {Symbol: "b"},

        {Symbol: "c"},

        {Symbol: "d"},

    }

}


func RunIntradayScanner() {

    symbols := GetSymbols()

    var intradayChannel = make(chan StratNotification)

    for _, s := range symbols {

        go IntradayStratify(strings.TrimSpace(s.Symbol), intradayChannel)

    }


    for _ = range symbols {

        s := <-intradayChannel

        fmt.Println(s)

    }

}


func IntradayStratify(ticker string, c chan StratNotification) {

    // do some heavy lifting

    fmt.Println(ticker)

    c <- StratNotification{}

}


func main() {

    RunIntradayScanner()

}


查看完整回答
反對 回復 2022-10-10
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號