문제

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