if no reactor use synch query

This commit is contained in:
dan sinclair 2010-11-26 22:42:34 -05:00
parent 9d49728c30
commit dac2f02c9b
2 changed files with 22 additions and 13 deletions

View File

@ -23,10 +23,14 @@ module Mysql2
end end
def query(sql, opts={}) def query(sql, opts={})
super(sql, opts.merge(:async => true)) if ::EM.reactor_running?
deferable = ::EM::DefaultDeferrable.new super(sql, opts.merge(:async => true))
::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true deferable = ::EM::DefaultDeferrable.new
deferable ::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true
deferable
else
super(sql, opts)
end
end end
end end
end end

View File

@ -1,23 +1,28 @@
# encoding: utf-8 # encoding: utf-8
require 'mysql2/em' require 'mysql2/em'
require 'fiber' unless defined? Fiber require 'fiber'
module Mysql2 module Mysql2
module EM module EM
module Fiber module Fiber
class Client < ::Mysql2::EM::Client class Client < ::Mysql2::EM::Client
def query(sql, opts={}) def query(sql, opts={})
deferable = super(sql, opts) if ::EM.reactor_running?
deferable = super(sql, opts)
fiber = ::Fiber.current fiber = ::Fiber.current
deferable.callback do |result| deferable.callback do |result|
fiber.resume(result) fiber.resume(result)
end
deferable.errback do |err|
p err
fiber.resume(err)
end
::Fiber.yield
else
super(sql, opts)
end end
deferable.errback do |err|
fiber.resume(err)
end
::Fiber.yield
end end
end end
end end