登录
首页 >  Golang >  Go问答

处理io.Pipe时出现死锁问题

来源:stackoverflow

时间:2024-03-15 11:54:30 386浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《处理io.Pipe时出现死锁问题》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我花了几个小时尝试理解底层逻辑,但没有任何进展。下面的代码在第一次迭代后返回死锁。如果我在 io.copy 之前关闭 writer,死锁就会消失,但不会打印任何内容(因为管道写入端在读取之前已关闭)

func main() {
    reader, writer := io.pipe()
    c := make(chan string)

    go func() {
        for i := 0; i < 5; i++ {
            text := fmt.sprintf("hello %vth time", i+1)
            c <- text
        }

        close(c)
    }()

    for msg := range c {
        msg = fmt.sprintf("\nreceived from channel -> %v\n", msg)

        go fmt.fprint(writer, msg)
        io.copy(os.stdout, reader)
        writer.close()
    }

}

这是运行代码后的错误

received from channel -> hello 1th time fatal error: all goroutines
are asleep - deadlock!
goroutine 1 [select]: io.(*pipe).read(0xc000130120, {0xc00013e000,
0x8000, 0xc00011e001?})
/usr/lib/go/src/io/pipe.go:57 +0xb1 io.(*PipeReader).Read(0x0?, {0xc00013e000?, 0xc00011e050?, 0x10?})
/usr/lib/go/src/io/pipe.go:136 +0x25 io.copyBuffer({0x4bde98, 0xc00011e050}, {0x4bddb8, 0xc00012e018}, {0x0, 0x0, 0x0})
/usr/lib/go/src/io/io.go:427 +0x1b2 io.Copy(...)
/usr/lib/go/src/io/io.go:386 os.genericReadFrom(0x101c00002c500?, {0x4bddb8, 0xc00012e018})
/usr/lib/go/src/os/file.go:161 +0x67 os.(*File).ReadFrom(0xc00012e008, {0x4bddb8, 0xc00012e018})
/usr/lib/go/src/os/file.go:155 +0x1b0 io.copyBuffer({0x4bde38, 0xc00012e008}, {0x4bddb8, 0xc00012e018}, {0x0, 0x0, 0x0})
/usr/lib/go/src/io/io.go:413 +0x14b io.Copy(...)
/usr/lib/go/src/io/io.go:386 main.pipetest()
/home/stranger/source-code/golang/ipctest/pipes/main.go:39 +0x1ae main.main()
/home/stranger/source-code/golang/ipctest/pipes/main.go:10 +0x17
goroutine 18 [chan send]: main.pipetest.func1()
/home/stranger/source-code/golang/ipctest/pipes/main.go:29 +0x85 created by main.pipetest
/home/stranger/source-code/golang/ipctest/pipes/main.go:26 +0x17a exit status 2

正确答案


io.copy 不断尝试复制,直到读取器到达 eof(在本例中,当管道关闭时)。由于您在 io.copy 结束后调用 writer.close() after ,因此 io.copy 将永远不会看到 eof,并永远挂起。

代码的另一个问题是您尝试多次关闭管道(每次循环代码重复时)。一般情况下,closeable 对象只应关闭一次,并且在 closed 后被假定为不可用。如果您需要重新使用它们,您应该创建一个新实例。

这是代码的工作修订版:

func main() {
    c := make(chan string)

    go func() {
        for i := 0; i < 5; i++ {
            text := fmt.Sprintf("hello %vth time", i+1)
            c <- text
        }

        close(c)
    }()

    for msg := range c {
        msg = fmt.Sprintf("\nreceived from channel -> %v\n", msg)

        // Create a new pipe for this message.
        reader, writer := io.Pipe()
        go func() {
            fmt.Fprint(writer, msg)
            // Close the pipe after writing the message.
            writer.Close()
        }()

        io.Copy(os.Stdout, reader)
    }
}

到这里,我们也就讲完了《处理io.Pipe时出现死锁问题》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>