<分区>
我正在使用 RubyPython
导入 Python 模块。我在构造函数中执行 RubyPython.start
(initialize
),我想我应该在析构函数中对称地执行 RubyPython.stop
,但不幸的是好像Ruby中没有析构函数:
class QDSHiveHelper
def initialize
RubyPython.start
qds = RubyPython.import('blah')
...
end
def do_something
qds.some_function
...
end
def finalize
RubyPython.stop
end
end
有人可以解释一下如何完成这个吗? ObjectSpace.define_finalize
似乎不被鼓励并且有一些陷阱(不能使用闭包等)。我也可以让 RubyPython
悬空而不对其调用 stop
,但我不知道会产生什么后果。最好的出路是什么?