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={})
if ::EM.reactor_running?
super(sql, opts.merge(:async => true)) super(sql, opts.merge(:async => true))
deferable = ::EM::DefaultDeferrable.new deferable = ::EM::DefaultDeferrable.new
::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true ::EM.watch(self.socket, Watcher, self, deferable).notify_readable = true
deferable deferable
else
super(sql, opts)
end
end end
end end
end end

View File

@ -1,13 +1,14 @@
# 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={})
if ::EM.reactor_running?
deferable = super(sql, opts) deferable = super(sql, opts)
fiber = ::Fiber.current fiber = ::Fiber.current
@ -15,9 +16,13 @@ module Mysql2
fiber.resume(result) fiber.resume(result)
end end
deferable.errback do |err| deferable.errback do |err|
p err
fiber.resume(err) fiber.resume(err)
end end
::Fiber.yield ::Fiber.yield
else
super(sql, opts)
end
end end
end end
end end