Ruby 中的对象销毁通知 [英] Notification of object destruction in Ruby

查看:23
本文介绍了Ruby 中的对象销毁通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个自定义 Rails 模型.这个模型是由一个实际的服务器而不是一个数据库表支持的(所以它不是从 ActiveRecord::Base 继承的).为了从服务器获取请求的信息,我打开了一个到它的 SSH 连接.因为 rails 不重用对象,所以会为收到的请求创建一个新对象以及与服务器的新 SSH 连接.为了减少服务器压力,我想在模型对象被垃圾收集之前关闭 SSH 连接.我想知道 ruby​​ 是否提供通知机制来通知对象它将被销毁?如果是这样,我可以使用此通知来了解何时关闭 SSH 连接.如果没有,当我知道我完成了对象时,我需要手动进行.

I have written a custom Rails model. This model is backed by an actually server not by a database table (so it does not inherit from ActiveRecord::Base). In order to get the requested information from the server I open a SSH connection to it. Because rails does not reuse object a new object, as well as a new SSH connection to the server, will be created for ever request that is received. To reduce server stress I want to close the SSH connection before the model object gets garbage collected. I am wondering does ruby provide a notification mechanism to inform the object that it will be destroyed? If so I could use this notification to know when to close the SSH connections. If not I will need to do it manual when I know I am finished with the object.

如果我需要手动处理这个问题,我可以显式销毁该对象吗?或者我能做的最好的事情是 object = nil?

If I need to manually take care of this, can I explicitly destroy the object? Or is the best i can do is object = nil?

推荐答案

如果你需要控制一个对象被销毁时会发生什么,你真的应该自己明确地销毁它——这是设计使然.您也不应该明确地销毁对象 - 这也是设计使然.

If you need to control what happens when an object is destroyed, you really should be explicitly destroying it yourself - this is by design. You're not supposed to be able to destroy an object explicitly either - this is also by design.

换句话说,从您的程序的角度来看,一个对象永远不会被销毁或可销毁.出于这些原因,您应该重新考虑问题(这并非罕见的需求 - 在不再需要对象时释放资源),以便它适合 Ruby 范式.

In other words, from the perspective of your program, an object is never destroyed or destroyable. For these reasons you should re-think the problem (this is not an uncommon need - release of resources when the object is no longer needed) so it fits into the Ruby paradigm.

将对象设置为 nil 给垃圾收集器一个提示,但不一定立即销毁它.

Setting the object to nil gives a hint to the garbage collector, but does not necessarily immediately destroy it.

但是,如果您必须让垃圾收集器处理它,请继续阅读.

However, if you must have the garbage collector handle it, then read on.

没有直接支持析构函数,但你可以让它调用 终结器函数 当它被销毁时.

There is no direct support for a destructor, but you can have it call a finalizer function when it is destroyed.

根据 http://pleac.sourceforge.net/pleac_ruby/classesetc.html 如果它包含对原始对象的引用,则它可能不会被垃圾回收,因此必须是类方法而不是实例方法.

According to http://pleac.sourceforge.net/pleac_ruby/classesetc.html it may not be garbage collected if it contains a reference to the original object, so must be a class method and not an instance method.

class MyClass
    def initialize
        ObjectSpace.define_finalizer(self,
                                     self.class.method(:finalize).to_proc)
    end
    def MyClass.finalize(id)
        puts "Object #{id} dying at #{Time.new}"
    end
end

这篇关于Ruby 中的对象销毁通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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