Ruby 的 File.open 和 f.close 的需要 [英] Ruby's File.open and the need for f.close

查看:48
本文介绍了Ruby 的 File.open 和 f.close 的需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数编程语言都知道处理文件的流程是打开-使用-关闭.然而,我在 ruby​​ 代码中多次看到无与伦比的 File.open 调用,而且我发现了这个知识宝库 在 ruby​​ 文档中:

It's common knowledge in most programming languages that the flow for working with files is open-use-close. Yet I saw many times in ruby codes unmatched File.open calls, and moreover I found this gem of knowledge in the ruby docs:

I/O 流在被垃圾收集器声明时会自动关闭.

I/O streams are automatically closed when they are claimed by the garbage collector.

darkredandyellow 友好的 irc 对这个问题的看法:
[17:12] 是的,而且文件描述符的数量通常受操作系统限制
[17:29] 我假设你很容易在垃圾收集器清理之前用完可用的文件描述符.在这种情况下,您可能想自己使用 close 它们.被垃圾收集者认领."意味着 GC 在未来的某个时间点起作用.而且很贵.明确关闭文件的很多原因.

darkredandyellow friendly irc take on the issue:
[17:12] yes, and also, the number of file descriptors is usually limited by the OS
[17:29] I assume you can easily run out of available file descriptors before the garbage collector cleans up. in this case, you might want to use close them yourself. "claimed by the garbage collector." means that the GC acts at some point in the future. and it's expensive. a lot of reasons for explicitly closing files.

  1. 我们是否需要明确关闭
  2. 如果是,那么为什么 GC 会自动关闭?
  3. 如果不是,那么为什么要选择?

推荐答案

我在 ruby​​ 代码中多次看到不匹配的 File.open 调用

能举个例子吗?我只在新手编写的代码中看到这一点,他们缺乏大多数编程语言的常识,即处理文件的流程是打开-使用-关闭".

Can you give an example? I only ever see that in code written by newbies who lack the "common knowledge in most programming languages that the flow for working with files is open-use-close".

有经验的 Rubyists 要么明确关闭他们的文件,要么更惯用地使用 File.open,它会自动为您关闭文件.它的实现基本上是这样的:

Experienced Rubyists either explicitly close their files, or, more idiomatically, use the block form of File.open, which automatically closes the file for you. Its implementation basically looks something like like this:

def File.open(*args, &block)
  return open_with_block(*args, &block) if block_given?
  open_without_block(*args)
end

def File.open_without_block(*args)
  # do whatever ...
end

def File.open_with_block(*args)
  yield f = open_without_block(*args)
ensure
  f.close
end

脚本是一个特例.脚本通常运行时间很短,并且使用的文件描述符很少,因此关闭它们根本没有意义,因为在脚本退出时操作系统无论如何都会关闭它们.

Scripts are a special case. Scripts generally run so short, and use so few file descriptors that it simply doesn't make sense to close them, since the operating system will close them anyway when the script exits.

我们需要明确关闭吗?

是的.

如果是,那么为什么 GC 会自动关闭?

If yes then why does the GC autoclose?

因为它收集了对象后,您将无法再关闭该文件,因此会泄漏文件描述符.

Because after it has collected the object, there is no way for you to close the file anymore, and thus you would leak file descriptors.

请注意,关闭文件的不是垃圾收集器.垃圾收集器只是在收集对象之前执行对象的任何终结器.碰巧 File 类定义了一个关闭文件的终结器.

Note that it's not the garbage collector that closes the files. The garbage collector simply executes any finalizers for an object before it collects it. It just so happens that the File class defines a finalizer which closes the file.

如果不是,那么为什么要选择?

If not then why the option?

因为浪费的内存很便宜,但浪费的文件描述符不是.因此,将文件描述符的生命周期与某个内存块的生命周期联系起来是没有意义的.

Because wasted memory is cheap, but wasted file descriptors aren't. Therefore, it doesn't make sense to tie the lifetime of a file descriptor to the lifetime of some chunk of memory.

您根本无法预测什么时候垃圾收集器将运行.你甚至无法预测是否它会运行:如果你永远不会耗尽内存,垃圾收集器永远不会运行,因此终结器永远不会运行,因此文件永远不会关闭.

You simply cannot predict when the garbage collector will run. You cannot even predict if it will run at all: if you never run out of memory, the garbage collector will never run, therefore the finalizer will never run, therefore the file will never be closed.

这篇关于Ruby 的 File.open 和 f.close 的需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆