
php小编子墨在这篇文章中将为您解答关于Golang中使用os.File.Fd()时SetDeadline/SetReadDeadline/SetWriteDeadline对文件不起作用的问题。在Golang中,这些方法是用来设置文件的截止时间,但是有时候可能会出现无效的情况。接下来,我们将探讨可能的原因,并提供解决方案来确保这些方法正常工作。
我使用 os.File.SetReadDeadline 和 os.File.ReadFull 的组合。但即使使用 SetReadDeadline,我设置的截止日期也被完全忽略,并且 ReadFull 永远阻塞。这是为什么?
其他信息:我向文件触发了一些 IOCTLS,因此需要 os.File.Fd() 来获取文件描述符。
tl;博士:
立即学习“go语言免费学习笔记(深入)”;
使用 os.file.fd() 后,在文件上使用 syscall.setnonblock(fd.fd(), true)
这是由于 read 在 golang unix 中:
func (fd *fd) read(p []byte) (int, error) {
if err := fd.readlock(); err != nil {
return 0, err
}
defer fd.readunlock()
if len(p) == 0 {
// if the caller wanted a zero byte read, return immediately
// without trying (but after acquiring the readlock).
// otherwise syscall.read returns 0, nil which looks like
// io.eof.
// todo(bradfitz): make it wait for readability? (issue 15735)
return 0, nil
}
if err := fd.pd.prepareread(fd.isfile); err != nil {
return 0, err
}
if fd.isstream && len(p) > maxrw {
p = p[:maxrw]
}
for {
n, err := ignoringeintrio(syscall.read, fd.sysfd, p)
if err != nil {
n = 0
if err == syscall.eagain && fd.pd.pollable() {
if err = fd.pd.waitread(fd.isfile); err == nil {
continue
}
}
}
err = fd.eoferror(n, err)
return n, err
}
}如果文件设置为阻塞模式,第一个 n, err := ignoringeintrio(syscall.read, fd.sysfd, p) 将永远阻塞。 waitread 仅当文件以非阻塞模式打开时才会执行。但我确实以非阻塞模式打开了文件,那么发生了什么?
os.file.fd()的实现 破坏了它:
func (f *File) Fd() uintptr {
if f == nil {
return ^(uintptr(0))
}
// If we put the file descriptor into nonblocking mode,
// then set it to blocking mode before we return it,
// because historically we have always returned a descriptor
// opened in blocking mode. The File will continue to work,
// but any blocking operation will tie up a thread.
if f.nonblock {
f.pfd.SetBlocking()
}
return uintptr(f.pfd.Sysfd)
}fd() 始终将文件设置为阻塞。因此,我们必须在等待轮询读取之前撤消该操作。因此:
使用 os.file.fd() 后,在文件上使用 syscall.setnonblock(fd.fd(), true)
以上就是GOLANG:为什么在使用 os.File.Fd() 时 SetDeadline/SetReadDeadline/SetWriteDeadline 对文件不起作用?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号