Multiple Ruby EventMachines in one process: maybe?

I have a situation where I want to run multiple EventMachines in Ruby - does anyone have experience with this? (I can write a test case to do it myself, if not. Stay with us).

Let's be clear: I want to create two threads myself and call EventMachine.run on both threads, so I really have two reactor loops.

The reason is because I am writing an asynchronous message bus with the AMQP gem that uses EventMachine. This is great, but I want to make it a separate modular component that can be used in two applications:

  • which has its own gui lock cycle (which cannot be simulated by calling a tick from EventMachine - it really blocks, and it does this in the C library, so I can't crack it). It's simple: just start EM in your thread and share the incoming messages between loops in a thread-safe manner;
  • and another application that itself works in the reactor loop, and I can share it with the AMQP code (which is good for thread safety problems), although I should still refer to the above application. This is what made me think ... Can I share the message bus code with the above application by running two separate EventMachines?

Anyone have any thoughts?

+6
source share
3 answers

OK, delving into EM documents, I see the body of EventMachine.run starting with this:

 240: if reactor_running? 241: (b = blk || block) and b.call # next_tick(b) 242: else ... start the reactor ... 

This is amazing. It looks like if you are doing EventMachine.run in multiple threads, he plans to schedule a second machine definition - the block passed to "run" - on the already running reactor.

I like this library.

+6
source

You answered yourself, but I wanted to add my 2 cents without a terrible comment style.

 # 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 
+2
source

if multiple servers are running in the same EM, some problems may arise. perhaps you can run em in multiple processes so as not to use the same em

-1
source

Source: https://habr.com/ru/post/902244/


All Articles