In Ruby-land this pattern is even simpler to implement:
require 'rubygems'
require 'sinatra'
$sum = 0
Thread.new do # trivial example work thread
while true do
sleep 0.12
$sum += 1
end
end
get '/' do
"Testing background work thread: sum is #{$sum}"
endWhile the main thread is waiting for HTTP requests the background thread can do any other work. This works fine with Ruby 1.8.7 or any 1.9.*, but I would run this in JRuby for a long-running production app since JRuby uses the Java Thread class.
0 comments:
Post a Comment