duanaozhong0696 2017-07-15 20:35
浏览 42
已采纳

在Go中压缩来自阅读器的数据

I have the following short program written in Go, which attempts to transparently compress the data in a reader (https://play.golang.org/p/SnvYT6it5r):

package main

import (
    "fmt"
    "io"
    "bytes"
    "compress/gzip"
)

func main() {
    data := bytes.NewReader([]byte("hello world"))
    compress(data)
}

func compress(data io.Reader) (io.Reader, error) {
    pr, pw := io.Pipe()
    gw := gzip.NewWriter(pw)

    n, err := io.Copy(gw, data)

    if err != nil {
        fmt.Printf("error: %s", err.Error())
    } else {
        fmt.Printf("%d bytes compressed", n)
    }
    return pr, err
}

When I run it, the program hangs:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_notifyListWait(0x1043e6cc, 0x0)
    /usr/local/go/src/runtime/sema.go:297 +0x140
sync.(*Cond).Wait(0x1043e6c4, 0x137118)
    /usr/local/go/src/sync/cond.go:57 +0xc0
io.(*pipe).write(0x1043e680, 0x1045a055, 0xa, 0xa, 0x0, 0x0, 0x0, 0x101)
    /usr/local/go/src/io/pipe.go:90 +0x1a0
io.(*PipeWriter).Write(0x1040c180, 0x1045a055, 0xa, 0xa, 0xe205ef63, 0x34c, 0x0, 0x0)
    /usr/local/go/src/io/pipe.go:157 +0x40
compress/gzip.(*Writer).Write(0x1045a000, 0x1040a130, 0xb, 0x10, 0x2c380, 0x7654, 0x1059e0, 0x111480)
    /usr/local/go/src/compress/gzip/gzip.go:168 +0x2e0
bytes.(*Reader).WriteTo(0x10440240, 0x190610, 0x1045a000, 0x0, 0xfef64000, 0x10440240, 0x1045a001, 0x190670)
    /usr/local/go/src/bytes/reader.go:134 +0xe0
io.copyBuffer(0x190610, 0x1045a000, 0x1905d0, 0x10440240, 0x0, 0x0, 0x0, 0x106620, 0x1045a000, 0x0, ...)
    /usr/local/go/src/io/io.go:380 +0x360
io.Copy(0x190610, 0x1045a000, 0x1905d0, 0x10440240, 0x10440240, 0x0, 0x1a47c0, 0x0)
    /usr/local/go/src/io/io.go:360 +0x60
main.compress(0x1905d0, 0x10440240, 0x10440240, 0x1040c170, 0x1040a130, 0xb)
    /tmp/sandbox403912545/main.go:19 +0x180
main.main()
    /tmp/sandbox403912545/main.go:12 +0xe0

What is causing the deadlock, and what is the most efficient way to compress data from a reader?

  • 写回答

1条回答 默认 最新

  • duanhe4155 2017-07-15 20:48
    关注

    You write to io.Pipe but you never read from it (in a parallel go routine), hence the deadlock. Here is what the docs say:

    Reads and Writes on the pipe are matched one to one except when multiple Reads are needed to consume a single Write. That is, each Write to the PipeWriter blocks until it has satisfied one or more Reads from the PipeReader that fully consume the written data. The data is copied directly from the Write to the corresponding Read (or Reads); there is no internal buffering.

    https://golang.org/pkg/io/#Pipe

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

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起