我有一个简单的Sinatra应用程序,通过Phusion Passenger在Apache上运行。Sinatra和Phusion乘客的线程行为
当应用程序启动时,我启动一个Ruby线程,每分钟执行一次繁重的计算,将结果留在全局变量中(然后在其他位置访问)。
当使用rackup
时,该变量每分钟更新和刷新一次,但在Passenger下运行时,似乎没有这样做。
# Seed the initial license data - on Sinatra starting - and
# set it on a global variable.
$license_data = generate_license_data(360)
# Start up a worker thread that will update the license data
# every 60 seconds. This thread will run until termination
# of the parent thread. Only this thread will modify the values
# of the global variable "license_data".
worker_thread = Thread.new do
while true
sleep 60
t = Time.now
print "Generating license data ..."
$license_data = generate_license_data(360)
print " OK (#{seconds_to_string(Time.now-t)})\n"
end
end
# Generate the actual HTML snippet we need for the license entry
# by accessing the global variable "license_data".
def generate_license_entry
# The license block.
@licensechart = {}
@licensechart[:values] = $license_data[:values_string]
# Generate the table entry.
haml :license
end
任何想法?我也很乐意知道另一种缓存计算的更好方法,以及如何每分钟更新一次。
嗯。我现在看着[delayed_job](http://rubygems.org/gems/delayed_job),正如SO提出的[question](http://stackoverflow.com/questions/3268832/is-it-a-坏主意到创建工作者线程-IN-A-服务器进程)。 – Dan