我正嘗試在我的rails應用程序中與Matlab.Application.Single win32ole對象進行交互。我遇到的問題是,當我開發我的應用程序時,每個單獨的請求都會重新加載我的win32ole對象,所以我沒有連接到我的matlab orignal實例並創建新的實例。有沒有辦法在rails中的請求之間保持活動對象?或者有沒有辦法重新連接到我的Matlab.Application.Single實例?rails在開發模式下堅持對象在請求上
在生產模式下,我使用模塊變量來存儲我的請求之間的連接,但是在開發模式下,每次請求都會重載模塊變量。
這裏是我的代碼片段
require 'win32ole'
module Calculator
@engine2 = nil
@engine3 = nil
def self.engine2
if @engine2.nil?
@engine2 = WIN32OLE.new("Matlab.Application.Single")
@engine2.execute("run('setup_path.m')")
end
@engine2
end
def self.engine3
if @engine3.nil?
@engine3 = WIN32OLE.new("Matlab.Application.Single")
@engine3.execute("run('setup_path.m')")
end
@engine3
end
def self.load_CT_image(file)
Calculator.engine2.execute("spm_image('Init','#{file}')")
end
def self.load_MR_image(file)
Calculator.engine3.execute("spm_image('Init','#{file}')")
end
end
我就能夠用我的代碼在我的控制器是這樣的:
Calculator.load_CT_image('Post_Incident_CT.hdr')
Calculator.load_MR_image('Post_Incident_MRI.hdr')
非常感謝,我正在嘗試將你的答案投票,但我需要先獲得更多的信譽積分。當我有這些時,會回來並給你信用額度,再次感謝。 – jnedrud