Ruby中对象销毁的通知 [英] Notification of object destruction in Ruby
问题描述
我写了一个自定义的Rails模型。这个模型由一个实际的服务器而不是数据库表支持(所以它不能从 ActiveRecord :: Base
继承)。为了从服务器获取请求的信息,我打开一个SSH连接。因为rails不会重用对象,所以会为接收到的任何请求创建一个新对象,以及到服务器的新SSH连接。为了减少服务器压力,我希望在模型对象被垃圾收集之前关闭SSH连接。我想知道ruby是否提供通知机制来通知对象它将被销毁?如果是这样,我可以使用此通知来了解何时关闭SSH连接。如果没有,我将需要做手动,当我知道我完成了对象。
如果我需要手动照顾这个,我可以显式销毁对象?或者是我能做的最好的是 object = nil
?
如果你需要控制一个对象被破坏时会发生什么,你应该自己明确地销毁它 - 这是设计。你不应该明确地销毁一个对象 - 这也是设计。
换句话说,从程序的角度来看,一个对象是从未销毁或可销毁。由于这些原因,您应该重新考虑这个问题(这不是一个不常见的需求 - 当不再需要对象时释放资源),因此它适合于Ruby范例。
将对象设置为nil会给垃圾收集器一个提示,但不一定会立即销毁它。
然而,如果您必须让垃圾收集器处理它,然后继续阅读。没有直接支持析构函数,但可以调用 finalizer function 当它被销毁时。
根据 http://pleac.sourceforge.net/pleac_ruby/classesetc.html 它可能不是垃圾收集,如果它包含对原始对象的引用,所以必须是类方法而不是实例方法。
class MyClass
def初始值ize
ObjectSpace.define_finalizer(self,
self.class.method(:finalize).to_proc)
end
def MyClass.finalize(id)
putsObject #{id}死于#{Time.new}
结束
结束
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.
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.
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.
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.
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屋!