Вопрос

Everybody always explicitly mentions that next_tick will be executed in the Main Thread. But what about Timers and callbacks/errbacks? Are they guaranteed to run in the Main Thread as well?

Это было полезно?

Решение

Regardless of Ruby version, EM runs everything except the EM.defer code block inside the reactor thread (EM.defer result and on error blocks are in the reactor too), so yes,

timers, callbacks and errbacks are guaranteed to run in the reactor (main) thread

Другие советы

Assuming you're using MRI Ruby, then there is only 1 'real/native' thread, which is the main/reactor thread, and all code is executed by this reactor thread. All timers and callbacks will be picked up on the reactor thread.

Eventmachine is not designed to use multiple threads as part of its standard operation, however, Eventmachine does have a 'EM::defer' method, which will execute the block on a different 'ruby' thread, and I believe that if you are running a multithread capable ruby such as jruby or rubinious, then this will be run on a 'native' OS thread. "Defer" should be used if you are going to be executing any 'blocking' code.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top