class EventMachine::ThreadedResource
end
completion
end
EM.do_something_with(result)
completion.callback do |result|
# This block will be yielded on the EM thread:
end
cassandra.get(:Things, ‘10’, ‘stuff’)
completion = dispatcher.dispatch do |cassandra|
# The dispatch block is executed in the resources thread.
pool.perform do |dispatcher|
# Example where we care about the result:
end
end
cassandra.insert(:Things, ‘10’, ‘stuff’ => ‘things’)
dispatcher.dispatch do |cassandra|
# access EventMachine things:
# The following block executes inside a dedicated thread, and should not
pool.perform do |dispatcher|
# If we don’t care about the result:
pool.add cassandra_dispatcher
pool = EM::Pool.new
end
Cassandra.new(‘allthethings’, ‘127.0.0.1:9160’)
cassandra_dispatcher = ThreadedResource.new do
pattern in rb_thread_select.
IO into EventMachines C++ IO implementations, instead relying on the reactor
in which normal synchronous code can occur, but makes no attempt to wire the
completion of nested operations. By contrast this approach provides a block
1.9. It also requires (potentially) complex stack switching logic to reach
EventMachine interface, but it’s sadly Fiber based and thus only works on
This example requires the cassandra gem. The cassandra gem contains an
== Basic Usage example
have a specific number of dedicated high-cpu worker resources.
General usage is to wrap libraries that do not support EventMachine, or to
async-ish“.
interfaces coherent and provide a simple approach at ”making an interface
wiring up synchronous code into a standard EM::Pool. This is useful to keep
A threaded resource is a “quick and dirty” wrapper around the concept of
= EventMachine::ThreadedResource
def dispatch
Called on the EM thread, generally in a perform block to return a
def dispatch completion = EM::Completion.new @queue << lambda do begin result = yield @resource completion.succeed result rescue => e completion.fail e end end completion end
def initialize
def initialize @resource = yield @running = true @queue = ::Queue.new @thread = Thread.new do @queue.pop.call while @running end end
def shutdown
Kill the internal thread. should only be used to cleanup - generally
def shutdown @running = false @queue << lambda {} @thread.join end