Reputation: 10554
I have a situation where I want to run multiple EventMachines in Ruby - does anyone have experience with this? (I may write a test case to do it myself if not. Stay tuned).
Let's be clear: I want to instantiate two threads myself, and call EventMachine.run
in both threads, so I really have two reactor loops.
The reason why is that I'm writing an asynchronous message bus with the AMQP gem, which uses EventMachine. That's fine, but I want to make that a separate, modular component that can be used within two applications:
Anybody have thoughts?
Upvotes: 9
Views: 2399
Reputation: 10554
OK, digging into EM's docs, I see the body for EventMachine.run starts with this:
240: if reactor_running?
241: (b = blk || block) and b.call # next_tick(b)
242: else
... start the reactor ...
This is awesome. It looks like, if you do EventMachine.run in multiple threads, it will schedule the second machine's definition - the block passed to "run" - on the reactor that is already running.
I love this library.
Upvotes: 9
Reputation: 1725
You answered yourself but I wanted to add my 2 cents without the horrible styling of comments.
# this will start the eventmachine reactor
EM::run do
# do something
# this will do nothing and the block passed to it will
# just be executed directly
EM::run do
# do something else
end
end
Upvotes: 4