Skip to content

Instantly share code, notes, and snippets.

@banker
Created March 17, 2011 17:32
Show Gist options
  • Save banker/874758 to your computer and use it in GitHub Desktop.
Save banker/874758 to your computer and use it in GitHub Desktop.
Forking without resetting the Connection.
require 'rubygems'
require 'mongo'
# A class to store our DB object
class Foo
def self.db
@@db ||= Mongo::Connection.new['stuff']
end
end
# Now insert some data
10.times do |n|
Foo.db['things'].save({:a => n})
end
# Now fork. You'll almost always see an exception here.
if !Kernel.fork
puts "This is the child!"
10.times do
p "Child 0: #{Foo.db['things'].find_one}"
end
else
puts "This is the parent"
10.times do
p "Parent: #{Foo.db['things'].find_one}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment