Skip to content

Instantly share code, notes, and snippets.

@jrichter
Created January 6, 2011 00:30
Show Gist options
  • Save jrichter/767305 to your computer and use it in GitHub Desktop.
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.
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