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

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

问题描述

在大多数编程语言中,常见的知识是使用文件的流程是开放使用的。然而,我在ruby代码中看到很多次无法匹敌的File.open调用,而且我发现这种知识宝库在ruby文档中:


I / O流在被垃圾回收器声明时自动关闭。 >

darkredandyellow 友好irc采取的问题:

[17:12]是的,文件描述符的数量通常受到操作系统的限制

[17:29]我假设您可以在垃圾收集器清理之前轻松耗尽可用的文件描述符。在这种情况下,您可能希望自己关闭它们。 垃圾收集机声称。意味着GC将来会在某个时候行动。这是昂贵的。很多原因明确地关闭文件。


  1. 我们需要明确关闭

  2. 如果是那么为什么GC自动关闭?

  3. 如果不是,为什么选项?


解决方案


我在ruby代码中看到很多次无法匹配的 File.open 调用


<你可以举个例子吗?我只会看到在新手们写的代码中,他们缺少
大多数编程语言中的常见知识,使用文件的流程是开放使用的。


经验丰富的Rubyists或者明确地关闭他们的文件,或者更习惯地使用 File.open ,它会为您自动关闭文件。它的实现基本上看起来像这样:

  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)
确保
f.close
end

脚本是一种特殊情况。脚本通常运行如此之短,并且使用这么少的文件描述符,因为关闭它们根本没有意义,因为当脚本退出时操作系统将关闭它们。



< blockquote>

我们需要明确关闭吗?


是的。


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


因为收集对象,你没有办法关闭文件,因此您将泄漏文件描述符。



请注意,垃圾收集器不是关闭文件。在收集对象之前,垃圾回收器只需执行任何终结器。它只是发生在 File 类定义一个关闭文件的终结器。


如果不是,为什么选项?


因为浪费的内存便宜,而是浪费了文件描述符。因此,将文件描述符的生命周期与某些内存块的生命周期相关联是没有意义的。



当您<垃圾收集器将运行。你甚至不能预测如果它将运行 :如果你永远不会运行内存,垃圾收集器将永远不会运行,因此终结器将永远不会运行,因此该文件永远不会关闭。


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 streams are automatically closed when they are claimed by the garbage collector.

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. Do we need to explicitly close
  2. If yes then why does the GC autoclose ?
  3. If not then why the option?

解决方案

I saw many times in ruby codes unmatched File.open calls

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".

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.

Do we need to explicitly close?

Yes.

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.

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天全站免登陆