When I io.Copy a file, it doesn't block and trying to use it might fail until it finishes

huangapple go评论76阅读模式
英文:

When I io.Copy a file, it doesn't block and trying to use it might fail until it finishes

问题

我有这段代码(或多或少):

resp, err := http.Get(url)
if err != nil {
	// 处理错误
}

if resp.StatusCode != http.StatusOK {
	// 处理错误
}

out, err := os.Create(filepath)
if err != nil {
	return err
}

// 将响应体写入文件
_, err = io.Copy(out, resp.Body)
resp.Body.Close()
out.Close()

我的问题是,如果我立即尝试做一些事情(例如,对该文件进行哈希处理),然后我会发现它仍然在复制一段时间。

起初,我将out.Close()延迟执行,我认为我需要在io.Copy之后关闭out,这样它会阻塞直到完成。但这并没有起作用,我仍然遇到同样的问题。

如何阻塞或等待io.Copy操作完成?

谢谢!

英文:

I have this code (more or less):

resp, err := http.Get(url)
if err != nil {
	// handle error
}

if resp.StatusCode != http.StatusOK {
	// handle error
}

out, err := os.Create(filepath)
if err != nil {
	return err
}

// Write the body to file
_, err = io.Copy(out, resp.Body)
resp.Body.Close()
out.Close()

My issue is that if I immediately try to do something (e.g. take the hash of this file), then I see that it is still copying for a while.

At first I was deferring the out.Close(), and I though that I need to out.Close after the io.Copy, which will block until its done with it. or so I thought.
This didn't work and I still have the same issue.

How do I block or wait for the io.Copy operation to finish?

Thanks!

答案1

得分: 4

可能你正在遇到一些磁盘缓冲/缓存问题,其中你的操作系统或磁盘设备在将数据实际写入磁盘之前将一些数据保存在内存中。

调用

out.Sync()

会强制执行一个 fsync 系统调用,这将指示操作系统强制刷新缓冲区并将数据写入磁盘。我建议在 io.Copy 调用返回之后调用 out.Flush()

你可能会对以下相关文档感兴趣:

英文:

Likely you are hitting some disk buffer/cache, where your OS or disk device keeps some data in memory before actually persisting the write to the disk.

Calling

out.Sync()

forces a fsync syscall, which will instruct the OS to force a flush of the buffer and write the data to disk. I suggest calling out.Flush() after the io.Copy call returns.

Related docs you may find interesting:

huangapple
  • 本文由 发表于 2021年7月22日 03:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/68475299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定