douke3007 2016-02-29 19:28
浏览 21
已采纳

我可以正确关闭此功能吗?

I keep getting an error after a few hours that I have too many open files.

I have already edited the ulimit -n but that only seems to prolong the time before the error occurs and crashes the process.

I believe I have narrowed it down to this function, I just am not sure if I am closing it right.

I currently have a function

go func() {
    if _, err := io.Copy(rw, stdout); err != nil {
        if !WritePipeBroken.MatchString(err.Error()) &&
            !ConnectionResetByPeer.MatchString(err.Error()) {
            rollbar.Error(rollbar.ERR, err)
        }
        log.Printf("pipeThruFfmpegToMp3: %v
", err)
        if err := ffmpeg.Process.Kill(); err != nil {
            log.Printf("pipeThruFfmpegToMp3: %v
", err)
        }
    }
    rw.Flush()
    wg.Done()
}()

I believe that it is hanging, so should I do this instead

go func() {
    if _, err := io.Copy(rw, stdout); err != nil {
        if !WritePipeBroken.MatchString(err.Error()) &&
            !ConnectionResetByPeer.MatchString(err.Error()) {
            rollbar.Error(rollbar.ERR, err)
        }
        log.Printf("pipeThruFfmpegToMp3: %v
", err)
        if err := ffmpeg.Process.Kill(); err != nil {
            log.Printf("pipeThruFfmpegToMp3: %v
", err)
        }
    }
    if ffmpeg.Process != nil {
        ffmpeg.Process.Kill()
    }
    if stdout != nil {
        stdin.Close()
    }
    rw.Flush()
    wg.Done()
}()
  • 写回答

1条回答 默认 最新

  • dongxing1853 2016-02-29 19:34
    关注

    use defer statement like google advises:

    func CopyFile(dstName, srcName string) (written int64, err error) {
        src, err := os.Open(srcName)
        if err != nil {
            return
        }
        defer src.Close()
    
        dst, err := os.Create(dstName)
        if err != nil {
            return
        }
        defer dst.Close()
    
        return io.Copy(dst, src)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 封装的 matplotlib animation 不显示图像
  • ¥15 python摄像头画面无法显示
  • ¥15 关于#3d#的问题:d标定算法(语言-python)
  • ¥15 cve,cnnvd漏洞扫描工具推荐
  • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错