Created
March 17, 2011 17:32
-
-
Save banker/874758 to your computer and use it in GitHub Desktop.
Forking without resetting the Connection.
This file contains hidden or 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
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