Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added standalone example.

  • Loading branch information...
commit 2493e1d95889c9114c9ebae770db8b42ff97268c 1 parent 4948d37
@cjbottaro cjbottaro authored
Showing with 55 additions and 0 deletions.
  1. +55 −0 examples/standalone.rb
View
55 examples/standalone.rb
@@ -0,0 +1,55 @@
+# Fill in your own spec for #establish_connection.
+
+require "eventmachine"
+require "fiber"
+require "active_record"
+require "benchmark"
+
+ActiveRecord::Base.logger = Logger.new(STDOUT)
+ActiveRecord::Base.establish_connection :adapter => "em_postgresql",
+ :port => 5432,
+ :pool => 2,
+ :username => "cjbottaro",
+ :host => "localhost",
+ :database => "template1",
+ :wait_timeout => 2
+
+def spawn(n)
+ n.times.collect do
+ Fiber.new do
+ begin
+ ActiveRecord::Base.connection.execute "select pg_sleep(1.1)"
+ ActiveRecord::Base.clear_active_connections!
+ rescue => e
+ puts e.inspect
+ end
+ end.tap{ |fiber| fiber.resume }
+ end
+end
+
+def join(fibers)
+ fibers.each do |fiber|
+ while fiber.alive?
+ current_fiber = Fiber.current
+ EM.next_tick{ current_fiber.resume }
+ Fiber.yield
+ end
+ end
+end
+
+EM.run do
+ Fiber.new do
+ time = Benchmark.realtime do
+ fibers = spawn(5)
+ join(fibers)
+
+ puts "first batch done"
+
+ fibers = spawn(5)
+ join(fibers)
+ end
+ puts time
+ EM.stop
+ end.resume
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.