在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,但更一般。我期待完全不同的答案。)