[rabbitmq-discuss] Ruby Gem with Unicorn server
michael.s.klishin at gmail.com
Tue Jan 17 18:16:58 GMT 2012
> I also have one other question regarding EventMachine#next_tick. I've read that:
> "Everything scheduled using EM#next_tick will happen synchronously in the main thread. Any long running tasks inside an EM#next_tick block will cause the entire program to block until complete. Typically a bad thing."
> Does this mean we don't gain anything by using EM in this fashion.. ie our attempt to use AMQP asynchronously is actually synchronous?
That piece of (EventMachine) documentation tries to say that EventMachine.next_tick always runs callbacks on the event loop thread (it does not necessary
have to be main thread. With Unicorn, Puma or any Web server that is not Thin, it won't be main). So you need to be careful to not block it. Web apps rarely
do anything that may do that.
EventMachine.next_tick used to delay any operations that use EventMachine until after event loop is ready. That's is all. It does not make amqp gem
synchronous. It just says "hey, EventMachine. I am not sure if you are running already, but when you are, run this code. Kthxbai".
So, to reiterate: with Unicorn, main Ruby VM thread is used by Unicorn itself. EventMachine is started in another thread. All network activity caused by amqp gem
always happens on that thread. In the context of Web apps that usually only publish events and commands, you typically never have to worry about it.
More information about the rabbitmq-discuss