第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Goroutines 同時(shí)加載大型 CSV 時(shí)的未定義行為

使用 Goroutines 同時(shí)加載大型 CSV 時(shí)的未定義行為

Go
哆啦的時(shí)光機(jī) 2023-05-04 17:47:07
我正在嘗試使用 Golang 使用 goroutines 加載一個(gè)大的 CSV 文件。csv 的維度是 (254882, 100)。但是當(dāng)我解析 csv 并將其存儲(chǔ)到 2D 列表中時(shí)使用我的 goroutines,我得到的行小于 254882 并且每次運(yùn)行的數(shù)量都不同。我覺(jué)得這是由于 goroutines 而發(fā)生的,但似乎無(wú)法指出原因。誰(shuí)能幫幫我嗎。我也是 Golang 的新手。下面是我的代碼func loadCSV(csvFile string) (*[][]float64, error) {    startTime := time.Now()    var dataset [][]float64    f, err := os.Open(csvFile)    if err != nil {        return &dataset, err    }    r := csv.NewReader(bufio.NewReader(f))    counter := 0    var wg sync.WaitGroup    for {        record, err := r.Read()        if err == io.EOF {            break        }        if counter != 0 {            wg.Add(1)            go func(r []string, dataset *[][]float64) {                var temp []float64                for _, each := range record {                    f, err := strconv.ParseFloat(each, 64)                    if err == nil {                        temp = append(temp, f)                    }                }                *dataset = append(*dataset, temp)                wg.Done()            }(record, &dataset)        }        counter++    }    wg.Wait()    duration := time.Now().Sub(startTime)    log.Printf("Loaded %d rows in %v seconds", counter, duration)    return &dataset, nil}我的主要功能如下所示func main() {    // runtime.GOMAXPROCS(4)    dataset, err := loadCSV("AvgW2V_train.csv")    if err != nil {        panic(err)    }    fmt.Println(len(*dataset))}如果有人也需要下載 CSV,請(qǐng)單擊下面的鏈接 (485 MB) https://drive.google.com/file/d/1G4Nw6JyeC-i0R1exWp5BtRtGM1Fwyelm/view?usp=sharing
查看完整描述

2 回答

?
鴻蒙傳說(shuō)

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

Go數(shù)據(jù)競(jìng)爭(zhēng)檢測(cè)器


您的結(jié)果未定義,因?yàn)槟袛?shù)據(jù)競(jìng)爭(zhēng)。

~/gopath/src$ go run -race racer.go

==================

WARNING: DATA RACE

Write at 0x00c00008a060 by goroutine 6:

? runtime.mapassign_faststr()

? ? ? /home/peter/go/src/runtime/map_faststr.go:202 +0x0

? main.main.func2()

? ? ? /home/peter/gopath/src/racer.go:16 +0x6a


Previous write at 0x00c00008a060 by goroutine 5:

? runtime.mapassign_faststr()

? ? ? /home/peter/go/src/runtime/map_faststr.go:202 +0x0

? main.main.func1()

? ? ? /home/peter/gopath/src/racer.go:11 +0x6a


Goroutine 6 (running) created at:

? main.main()

? ? ? /home/peter/gopath/src/racer.go:14 +0x88


Goroutine 5 (running) created at:

? main.main()

? ? ? /home/peter/gopath/src/racer.go:9 +0x5b

==================

fatal error: concurrent map writes

==================

WARNING: DATA RACE

Write at 0x00c00009a088 by goroutine 6:

? main.main.func2()

? ? ? /home/peter/gopath/src/racer.go:16 +0x7f


Previous write at 0x00c00009a088 by goroutine 5:

? main.main.func1()

? ? ? /home/peter/gopath/src/racer.go:11 +0x7f


Goroutine 6 (running) created at:

? main.main()

? ? ? /home/peter/gopath/src/racer.go:14 +0x88


Goroutine 5 (running) created at:

? main.main()

? ? ? /home/peter/gopath/src/racer.go:9 +0x5b

==================


goroutine 34 [running]:

runtime.throw(0x49e156, 0x15)

? ? /home/peter/go/src/runtime/panic.go:608 +0x72 fp=0xc000094718 sp=0xc0000946e8 pc=0x44b342

runtime.mapassign_faststr(0x48ace0, 0xc00008a060, 0x49c9c3, 0x8, 0xc00009a088)

? ? /home/peter/go/src/runtime/map_faststr.go:211 +0x46c fp=0xc000094790 sp=0xc000094718 pc=0x43598c

main.main.func1(0x49c9c3, 0x8)

? ? /home/peter/gopath/src/racer.go:11 +0x6b fp=0xc0000947d0 sp=0xc000094790 pc=0x47ac6b

runtime.goexit()

? ? /home/peter/go/src/runtime/asm_amd64.s:1340 +0x1 fp=0xc0000947d8 sp=0xc0000947d0 pc=0x473061

created by main.main

? ? /home/peter/gopath/src/racer.go:9 +0x5c


goroutine 1 [sleep]:

time.Sleep(0x5f5e100)

? ? /home/peter/go/src/runtime/time.go:105 +0x14a

main.main()

? ? /home/peter/gopath/src/racer.go:19 +0x96


goroutine 35 [runnable]:

main.main.func2(0x49c9c3, 0x8)

? ? /home/peter/gopath/src/racer.go:16 +0x6b

created by main.main

? ? /home/peter/gopath/src/racer.go:14 +0x89

exit status 2

~/gopath/src$?

racer.go:


package main


import (

? ? "bufio"

? ? "encoding/csv"

? ? "fmt"

? ? "io"

? ? "log"

? ? "os"

? ? "strconv"

? ? "sync"

? ? "time"

)


func loadCSV(csvFile string) (*[][]float64, error) {

? ? startTime := time.Now()

? ? var dataset [][]float64

? ? f, err := os.Open(csvFile)

? ? if err != nil {

? ? ? ? return &dataset, err

? ? }

? ? r := csv.NewReader(bufio.NewReader(f))

? ? counter := 0

? ? var wg sync.WaitGroup

? ? for {

? ? ? ? record, err := r.Read()

? ? ? ? if err == io.EOF {

? ? ? ? ? ? break

? ? ? ? }

? ? ? ? if counter != 0 {

? ? ? ? ? ? wg.Add(1)

? ? ? ? ? ? go func(r []string, dataset *[][]float64) {

? ? ? ? ? ? ? ? var temp []float64

? ? ? ? ? ? ? ? for _, each := range record {

? ? ? ? ? ? ? ? ? ? f, err := strconv.ParseFloat(each, 64)

? ? ? ? ? ? ? ? ? ? if err == nil {

? ? ? ? ? ? ? ? ? ? ? ? temp = append(temp, f)

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? *dataset = append(*dataset, temp)

? ? ? ? ? ? ? ? wg.Done()

? ? ? ? ? ? }(record, &dataset)

? ? ? ? }

? ? ? ? counter++

? ? }

? ? wg.Wait()

? ? duration := time.Now().Sub(startTime)

? ? log.Printf("Loaded %d rows in %v seconds", counter, duration)

? ? return &dataset, nil

}


func main() {

? ? // runtime.GOMAXPROCS(4)

? ? dataset, err := loadCSV("/home/peter/AvgW2V_train.csv")

? ? if err != nil {

? ? ? ? panic(err)

? ? }

? ? fmt.Println(len(*dataset))

}



查看完整回答
反對(duì) 回復(fù) 2023-05-04
?
狐的傳說(shuō)

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

沒(méi)有必要使用,*[][]float64因?yàn)槟菍⑹且粋€(gè)雙指針。


我對(duì)你的程序做了一些小的修改。


dataset可用于新的 goroutine,因?yàn)樗窃谒厦娴拇a塊中聲明的。


similarlyrecord也是可用的,但是由于recordvariable 是不時(shí)變化的,我們需要將它傳遞給新的 goroutine。


雖然不需要傳遞dataset,因?yàn)樗鼪](méi)有改變,而這正是我們想要的,這樣我們就可以將 temp 附加到dataset.


但是當(dāng)多個(gè) goroutines 試圖附加到同一個(gè)變量時(shí),就會(huì)發(fā)生競(jìng)爭(zhēng)條件,即多個(gè) goroutines 試圖寫入同一個(gè)變量。


所以我們需要確保在任何時(shí)候只有一個(gè) can goroutine 可以添加。所以我們使用鎖來(lái)進(jìn)行順序追加。


package main


import (

    "bufio"

    "encoding/csv"

    "fmt"

    "os"

    "strconv"

    "sync"

)


func loadCSV(csvFile string) [][]float64 {

    var dataset [][]float64


    f, _ := os.Open(csvFile)


    r := csv.NewReader(f)


    var wg sync.WaitGroup

    l := new(sync.Mutex) // lock


    for record, err := r.Read(); err == nil; record, err = r.Read() {

        wg.Add(1)


        go func(record []string) {

            defer wg.Done()


            var temp []float64

            for _, each := range record {

                if f, err := strconv.ParseFloat(each, 64); err == nil {

                    temp = append(temp, f)

                }

            }

            l.Lock() // lock before writing

            dataset = append(dataset, temp) // write

            l.Unlock() // unlock


        }(record)

    }


    wg.Wait()


    return dataset

}


func main() {

    dataset := loadCSV("train.csv")

    fmt.Println(len(dataset))

}

有些錯(cuò)誤沒(méi)有得到處理以使其最小化,但您應(yīng)該處理錯(cuò)誤。


查看完整回答
反對(duì) 回復(fù) 2023-05-04
  • 2 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)