Skip to content

Instantly share code, notes, and snippets.

@kapso
Created December 12, 2012 23:09
Show Gist options
  • Save kapso/4272531 to your computer and use it in GitHub Desktop.
Save kapso/4272531 to your computer and use it in GitHub Desktop.
def thread_write
pool = ThreadPool.new(80)
doc_size = 10000
doc_size.times do |i|
pool.process do
write_doc(i)
read_doc(i)
end
end
puts "Completed!!"
end
def connection(options = { pool: true })
if options[:pool]
Mongo::Connection.new("localhost", 27017, pool_size: 15, pool_timeout: 5)
else
Mongo::Connection.new("localhost", 27017)
end
end
def write_doc(id)
connection.db("pool_test_db", pool: true).collection("products").insert({ _id: "#{id}", name: "Nikon D#{id}" }, safe: true)
end
def read_doc(id)
doc = connection.db("pool_test_db").collection("products").find(_id: "#{id}").to_a.first
if doc
puts "\nTrying to read doc #{id}. Doc -> #{doc.inspect}\n"
else
puts "\nTrying to read doc #{id}. Doc -> IT IS NIL !!!!!!!\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment