在Ruby中,我有一個控制器操作,可立即啓動需要幾秒鐘的計算密集型任務。我希望客戶端輪詢我的服務器並獲取狀態更新。在軌中存儲臨時數據的位置
在我的控制器:
def complex_task
Thread.new do
loop do
one_part_of_the_computationally_intensive_task
# Note how much progress we've made, in case the client asks.
save_progress
end
end
# returns to client straightaway, but task continues in background.
end
def check_status
# Retrieve state in here.
get_progress
end
注check_status
需要怎麼拉一個由單獨的線程存儲的狀態。
如何編寫save_progress
和get_progress
函數?我在哪裏存儲狀態?看起來將它存儲在session
中並不是一個好主意,因爲這會發送給客戶端,但是我還可以在哪裏存儲它?
(與this question,但更一般。我期待完全不同的答案。)