英文:
How to continuously receive data from channel of goroutine
问题
我是Golang的初学者。我做了一个关于Go通道的练习。在主goroutine中,我打开并读取一个文件的数据,然后将数据通过通道传递给第二个goroutine,以保存到另一个文件中。
我的代码如下:
func main() {
f, err := os.OpenFile("test.go", os.O_RDONLY, 0600)
ch := make(chan []byte)
buf := make([]byte, 10)
bytes_len, err := f.Read(buf)
fmt.Println("ReadLen:", bytes_len)
if err != nil {
fmt.Println("Error:", err)
return
}
go WriteFile(ch)
for {
ch <- buf
bytes_len, err = f.Read(buf)
if err != nil {
fmt.Println("error=", err)
break
}
if bytes_len < 10 {
ch <- buf[:bytes_len]
fmt.Println("Finished!")
break
}
}
time.Sleep(1e9)
f.Close()
}
func WriteFile(ch <-chan []byte) {
fmt.Println("* begin!")
f, err := os.OpenFile("/home/GoProgram/test/test.file", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
fmt.Println("* Error:", err)
return
}
// Method 2: use "for {if}", this will get messed text in target file, not identical with the source file.
for {
if bytes, ok := <-ch; ok {
f.Write(bytes)
fmt.Println("* buff=", string(bytes))
bytes = nil
ok = false
} else {
fmt.Println("** End ", string(bytes), " ", ok)
break
}
}
f.Close()
}
我的问题是为什么方法2和方法3会在目标文件中得到混乱的文本?我该如何修复它?
英文:
I am a beginner of the Golang. I had made a practice about Go channel. Which I open and read data from a file in the main goroutine, then pass the data to the second goroutine to save to another file with channel.
My code is as flows
func main() {
f, err := os.OpenFile("test.go", os.O_RDONLY, 0600)
ch := make(chan []byte)
buf := make([]byte, 10)
bytes_len, err := f.Read(buf)
fmt.Println("ReadLen:", bytes_len)
if err != nil {
fmt.Println("Error: ", err)
return
}
go WriteFile(ch)
for {
ch<-buf
bytes_len, err = f.Read(buf)
if err != nil {
fmt.Println("error=", err)
break
}
if bytes_len < 10 {
ch<-buf[:bytes_len]
fmt.Println("Finished!")
break
}
}
time.Sleep(1e9)
f.Close()
}
func WriteFile(ch <-chan []byte) {
fmt.Println("* begin!")
f, err := os.OpenFile("/home/GoProgram/test/test.file", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
fmt.Println("* Error:", err)
return
}
/* Method1: use the "select" will write to target file OK, but it is too slow!!!
for {
select {
case bytes, ok:= <-ch:
if ok {
f.Write(bytes)
} else {
fmt.Println("* file closed!")
break
}
default:
fmt.Println("* waiting data!")
}
} \*/
// Method 2: use "for {if}", this will get messed text in target file, not identical with the source file.
for {
if bytes, ok := <-ch; ok {
f.Write(bytes)
fmt.Println("* buff=", string(bytes))
bytes = nil
ok = false
} else {
fmt.Println("** End ", string(bytes), " ", ok)
break
}
}
/* Method 3: use "for range", this will get messed text like in method2
for data:= range ch {
f.Write(data)
//fmt.Println("* Data:", string(data))
}
\*/
f.Close()
}
My question is why the Method2 and Method3 will get messed text in the target file? How can I fix it?
答案1
得分: 4
Method2和Method3的文本混乱是因为读取器和写入器共享的缓冲区存在竞争。
以下是上述程序可能的语句执行顺序:
R: bytes_len, err = f.Read(buf)
R: ch<-buf[:bytes_len]
W: bytes, ok := <-ch; ok
R: bytes_len, err = f.Read(buf) // 这会覆盖缓冲区
W: f.Write(bytes) // 从第二次读取写入数据
使用竞争检测器运行程序。它会为您标记出问题。
解决问题的一种方法是复制数据。例如,从读取的字节创建一个字符串,并将字符串发送到通道。
另一种选择是使用io.Pipe连接goroutine。一个goroutine从源读取并写入管道。另一个goroutine从管道读取并写入目标。管道会处理同步问题。
英文:
Method2 and Method3 get messed up text because there's a race on the buffer shared by the reader and writer.
Here's a possible sequence of statement execution for the program above:
R: bytes_len, err = f.Read(buf)
R: ch<-buf[:bytes_len]
W: bytes, ok := <-ch; ok
R: bytes_len, err = f.Read(buf) // this writes over buffer
W: f.Write(bytes) // writes data from second read
Run your program with the race dectector. It will flag the issues for you.
One way to fix the problem is to copy the data. For example, create a string from the bytes read and send the string to the channel.
Another option is to connect the goroutines with an io.Pipe. One goroutine reads from the source and writes to the pipe. The other goroutine reads from the pipe and writes to the destination. The pipe takes care of the synchronization issues.
答案2
得分: 1
为了使用你在注释中提到的Method2
和Method3
中的for循环来获取代码片段,你需要使用一个带有缓冲的通道。
导致目标文件中文本混乱的原因是func main
中的循环没有与WriteFile
中监听通道的循环同步的机制。
另一方面,向带有缓冲的通道发送数据只有在缓冲区满时才会阻塞。接收数据只有在缓冲区为空时才会阻塞。因此,通过初始化一个长度为1的缓冲通道,你可以使用Method1
和/或Method2
。唯一需要记住的是在完成后关闭通道。
func main() {
f, _ := os.OpenFile("test.txt", os.O_RDONLY, 0600)
defer f.Close()
ch := make(chan []byte, 1) // 使用make的第二个参数来指定缓冲区长度为1
buf := make([]byte, 10)
go WriteFile(ch)
for {
ch <- buf
byteLen, err := f.Read(buf)
if err != nil {
break
}
if byteLen < 10 {
ch <- buf[:byteLen]
break
}
}
close(ch) // 在完成后关闭通道
}
func WriteFile(ch <-chan []byte) {
f, err := os.OpenFile("othertest.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
defer f.Close()
if err != nil {
fmt.Println("* Error:", err)
return
}
// Method 3: 使用"for range"
for data := range ch {
f.Write(data)
}
}
英文:
In order to get the code snippets using the for loops in what you have put in comments as Method2
and Method3
, you will need to use a buffered channel.
The reason the text gets messed up in the target file is the loop in func main
has no mechanism to synchronize in lock step with the loops listening on the channel in WriteFile
.
Sends to a buffered channel, on the other hand, block only when the buffer is full. Receives block when the buffer is empty. So by initializing a channel with a buffer length of one, your can use Method1
and/or Method2
. All that's left is remembering to close the channel when you are done.
func main() {
f, _ := os.OpenFile("test.txt", os.O_RDONLY, 0600)
defer f.Close()
ch := make(chan []byte, 1) // use second argument to make to give buffer length 1
buf := make([]byte, 10)
go WriteFile(ch)
for {
ch <- buf
byteLen, err := f.Read(buf)
if err != nil {
break
}
if byteLen < 10 {
ch <- buf[:byteLen]
break
}
}
close(ch) //close the channel when you done
}
func WriteFile(ch <-chan []byte) {
f, err := os.OpenFile("othertest.txt", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
defer f.Close()
if err != nil {
fmt.Println("* Error:", err)
return
}
//Method 3: use "for range"
for data := range ch {
f.Write(data)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论