Friday, February 10, 2012

Parallel Processing in Ruby On Rails

I used ruby's fork when I was sending 60,000 emails from one of Rails applications and it is wise to split the big result set and send emails of each chunk parallely on forks for faster processing! Here is a sample ruby code for processing N length data.

fork do
ActiveRecord::Base.establish_connection
for i in 0..20000
puts 'I m in sub process-1'
end
exit
end
fork do
ActiveRecord::Base.establish_connection
for i in 2001..40000
puts 'I m in sub process-2'
end
exit
end
.......
fork do
ActiveRecord::Base.establish_connection
for i in 40000..N
puts 'I m in sub process-M'
end
exit
end
view raw gistfile1.txt hosted with ❤ by GitHub
I tested it on Ubuntu 11, ruby 1.9.2, Rails 3.0.10