Created
January 6, 2011 00:30
-
-
Save jrichter/767305 to your computer and use it in GitHub Desktop.
How I took advantage of concurrency. I found this somewhere and modified it. I don't remember where.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def do_work_concurrently(number_of_workers = 5) | |
config = ActiveRecord::Base.remove_connection | |
max = number_of_workers | |
pids = [] | |
any_work = true | |
while any_work do | |
# Don't forget to set any_work to false to exit the loop | |
pids << fork_with_new_connection(config) { | |
#Put your work here! | |
exit 99 # This is important if I do remember | |
} | |
while pids.length >= max | |
pids.each do |pid| | |
Process.waitpid(pid) | |
if $?.exited? == true | |
pids.delete pid | |
end | |
end | |
end | |
end | |
while pids.length != 0 | |
pids.each do |pid| | |
Process.wait(pid) | |
if $?.exited? == true | |
pids.delete pid | |
end | |
end | |
# I don't remember why I have two loops killing pid's, probably for cleanup | |
end | |
ActiveRecord::Base.establish_connection(config) | |
end | |
def self.fork_with_new_connection(config, klass = ActiveRecord::Base) | |
fork do | |
begin | |
klass.establish_connection(config) | |
yield | |
ensure | |
klass.remove_connection | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment