doumiang2297 2018-12-12 22:44
浏览 34
已采纳

GO例程中缓冲通道范围的输出中断

Why a GO routine like the following outputs sequences of bytes in a random order when using a buffered channel?

Here is the code to replicate the buggy behaviour, where data.csv is a simple CSV of 1000 rows of random data (100 bytes per row approximately) plus the header row (1001 rows in total).

package main

import (
    "bufio"
    "os"
    "time"
)

func main() {

    var channelLength = 10000
    var channel = make(chan []byte, channelLength)

    go func() {
        for c := range channel {
            println(string(c))
        }
    }()

    file, _ := os.Open("./data.csv")
    scanner := bufio.NewScanner(file)

    for scanner.Scan() {
        channel <- scanner.Bytes()
    }

    <-time.After(time.Second * time.Duration(3600))

}

Here are the first 6 lines of the output as an example of what I mean for "broken output":

979,C
tharine,Vero,cveror6@blinklist.com,Female,133.153.12.53
980,Mauriz
a,Ilett,milettr7@theguardian.com,Female,226.123.252.118
981
Sher,De Laci,sdelacir8@nps.gov,Female,137.207.30.217
[...]

On the other hand, the code runs smoothly if channelLength = 0, so with an unbuffered channel (first 6 lines, again):

id,first_name,last_name,email,gender,ip_address
1,Hebert,Edgecumbe,hedgecumbe0@apple.com,Male,108.84.217.38
2,Minor,Lakes,mlakes1@marriott.com,Male,231.185.189.39
3,Faye,Spurdens,fspurdens2@oakley.com,Female,80.173.161.81
4,Kris,Proppers,kproppers3@gmpg.org,Male,10.80.182.51
5,Bronnie,Branchet,bbranchet4@squarespace.com,Male,118.117.0.5
[...]

Data is random generated.

  • 写回答

1条回答 默认 最新

  • duanli6618 2018-12-12 22:48
    关注

    From the buffer.Scanner docs:

    The underlying array may point to data that will be overwritten by a subsequent call to Scan

    You have a data race around the use of the slices you're passing over the channel. You need to copy the data you're sending. In this example, that is most easily accomplished by using a string instead of []byte, and calling scanner.Text

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄