Ruby - As Streaming Through Kernels / Processors

Im (re) write the socket server in ruby ​​hoping to simplify it. While reading about ruby ​​sockets I came across a site that says multithreaded ruby ​​applications only use one core / cpu per machine.

Questions:

  • That's for sure?
  • I do not care? Each thread on this server will run for a few minutes, and there will be many. Is the OS (CentOS 6.5) smart enough to share the load?
  • Isn't this different from C ++ stream processing? (current socket server language) IE makes pthreads automatically use multiple cores?
  • What if I fork instead of a stream ?
+3


source to share


4 answers


CRuby has a global interpreter lock , so it cannot run threads in parallel. Jruby and some other implementations can do this, but CRuby will never run any code in parallel. This means that no matter how smart your OS is, it can never share the load.

On a stream in C ++, this is different. pthreads create real OS threads, and the kernel scheduler will run them on multiple cores at the same time. Technically Ruby also uses pthreads, but the GIL doesn't allow them to run in parallel.



Fork creates a new process, and the OS scheduler will almost certainly be smart enough to run it on a separate kernel. If you need parallelism in Ruby, either use a non-GIL implementation or use fork.

+3


source


Because of the GIL in YARV, ruby ​​is not thread friendly. If you want to write a multi-threaded ruby ​​use jruby or rubinius. It would be even better to use a functional language with an actor model like Erlang or Elixir and let the VM handle threads and you only control the Erlang processes.



+2


source


There is a very nice gem called parallel that allows you to process data with parallel threads or multiple processes by forking (working around the GIL of the current CRUBY).

+1


source


Threading

If you want multi-core streaming, you need to use an interpreter that makes heavy use of multiple cores. MRI Ruby from 2.1.3 is still only single-core; JRuby and Rubinius allow access to multiple cores.

Threading alternatives

Alternatives to changing your translator include:

  • DRb with multiple Ruby processes.
  • Multi-worker queuing system.
  • Socket programming with multiple interpreters.
  • Fork processes, if the underlying platform supports the fork (2) system call.
0


source







All Articles