在下面的代码中,对的调用io.Copy
永远不会返回;它只是无限期地阻塞,导致死锁。io.Pipe
仅当使用 来连接read
io.Reader
时,才会出现此行为os.Stdout
io.Writer
。但是,我需要使用管道,因为在我的完整代码中,我使用io.MultiWriter
with io.Pipe
s 将一个函数连接io.Reader
到多个需要io.Reader
.
func main() {
read := strings.NewReader("abcdefghij")
pipeReader, pipeWriter := io.Pipe()
var wg sync.WaitGroup
wg.Add(1)
go func() {
println("Start copy")
_, err := io.Copy(os.Stdout, pipeReader)
if err != nil {
println(err.Error())
}
println("End copy")
wg.Done()
}()
_, err := io.Copy(pipeWriter, read)
if err != nil {
println(err.Error())
}
wg.Wait()
}
Run Code Online (Sandbox Code Playgroud)
输出:
Start copy
abcdefghij
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc0000b0018?)
/usr/local/go-faketime/src/runtime/sema.go:62 +0x25
sync.(*WaitGroup).Wait(0x4969c8?)
/usr/local/go-faketime/src/sync/waitgroup.go:139 +0x52
main.main()
/tmp/sandbox4108076976/prog.go:31 +0x23c
goroutine 18 [select]:
io.(*pipe).read(0xc0000a6120, {0xc0000b6000, 0x8000, 0xc00009e101?})
/usr/local/go-faketime/src/io/pipe.go:57 +0xb1
io.(*PipeReader).Read(0x10?, {0xc0000b6000?, 0xc00009e1e0?, 0x4f75a0?})
/usr/local/go-faketime/src/io/pipe.go:136 +0x25
io.copyBuffer({0x496aa8, 0xc00009e1e0}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
/usr/local/go-faketime/src/io/io.go:427 +0x1b2
io.Copy(...)
/usr/local/go-faketime/src/io/io.go:386
os.genericReadFrom(0xb000000006018ab?, {0x4969a8, 0xc0000a4018})
/usr/local/go-faketime/src/os/file.go:162 +0x67
os.(*File).ReadFrom(0xc0000a4008, {0x4969a8, 0xc0000a4018})
/usr/local/go-faketime/src/os/file.go:156 +0x1b0
io.copyBuffer({0x496a28, 0xc0000a4008}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
/usr/local/go-faketime/src/io/io.go:413 +0x14b
io.Copy(...)
/usr/local/go-faketime/src/io/io.go:386
main.main.func1()
/tmp/sandbox4108076976/prog.go:18 +0x71
created by main.main
/tmp/sandbox4108076976/prog.go:16 +0x1d3
Run Code Online (Sandbox Code Playgroud)
这是代码的游乐场链接:https ://goplay.tools/snippet/70UbGIz8fTV
有没有办法在保持 的同时避免僵局io.Pipe
?
完成后关闭管道的写入端:
_, err := io.Copy(pipeWriter, read)
pipeWriter.Close()
if err != nil {
println(err.Error())
}
Run Code Online (Sandbox Code Playgroud)
否则,读者端将无限期地等待。