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

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

golang 系統調用,鎖定到線程

golang 系統調用,鎖定到線程

Go
富國滬深 2022-08-24 17:23:12
我正在嘗試創建一個程序來抓取xml文件。我正在嘗試圍棋,因為它是goroutines。我有幾千個文件,所以某種類型的多處理幾乎是必需的......我有一個程序成功運行,并將xml轉換為csv(作為測試,不完全是最終結果),在一組測試文件上,但是當使用完整的文件集運行時,它給出了這樣的:runtime: program exceeds 10000-thread limit我一直在尋找類似的問題,而且有幾個,但我還沒有找到一個足夠相似的問題來解決這個問題。最后,這里有一些代碼 im 運行:// main func (start threads)for i := range filelist {  channels = append(channels, make(chan Test))  go Parse(files[i], channels[len(channels)-1])}// Parse func (individual threads)func Parse(fileName string, c chan Test) {defer close(c)doc := etree.NewDocument()if err := doc.ReadFromFile(fileName); err != nil {    return}root := doc.SelectElement("trc:TestResultsCollection")for _, test := range root.FindElements("//trc:TestResults/tr:ResultSet/tr:TestGroup/tr:Test") {    var outcome Test    outcome.StepType = test.FindElement("./tr:Extension/ts:TSStepProperties/ts:StepType").Text()    outcome.Result = test.FindElement("./tr:Outcome").Attr[0].Value    for _, attr := range test.Attr {        if attr.Key == "name" {            outcome.Name = attr.Value        }    }    for _, attr := range test.FindElement("./tr:TestResult/tr:TestData/c:Datum").Attr {        if attr.Key == "value" {            outcome.Value = attr.Value        }    }    c <- outcome}}// main (process results when threads return)for c := 0; c < len(channels); c++ {    for i := range channels[c] {        // csv processing with i    }}我敢肯定那里有一些丑陋的代碼。我最近剛從其他語言中拿起go...所以我提前道歉。無論如何任何想法?
查看完整描述

1 回答

?
MMTTMM

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

我很抱歉沒有包括正確的錯誤。正如評論所指出的那樣,我正在做一些愚蠢的事情,并為每個文件創建一個例程。感謝JimB糾正我,并感謝torek提供解決方案和此鏈接。https://gobyexample.com/worker-pools


jobs := make(chan string, numJobs)

results := make(chan []Test, numJobs)


for w := 0; w < numWorkers; w++ {

    go Worker(w, jobs, results)

    wg.Add(1)

}


// give workers jobs


for _, i := range files {

    if filepath.Ext(i) == ".xml" {

        jobs <- ("Path to files" + i)

    }

}


close(jobs)

wg.Wait()


//result processing <- results


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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