Ruby-如何跨内核/处理器进行线程化 [英] Ruby - how to thread across cores / processors

查看:65
本文介绍了Ruby-如何跨内核/处理器进行线程化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(用红宝石)编写了一个套接字服务器,以期简化它.在阅读有关红宝石套接字的信息时,我遇到了站点,其中说多线程红宝石应用仅使用一个内核/处理器.机器.

Im (re)writing a socket server in ruby in hopes of simplifying it. Reading about ruby sockets I ran across a site that says multithreaded ruby apps only use one core / processor in a machine.

问题:

  1. 这准确吗?
  2. 我在乎吗?该服务器中的每个线程可能会运行几分钟,并且会有很多线程.操作系统(CentOS 6.5)是否足够智能以分担负载?
  3. 这与C ++中的线程有什么不同吗? (当前套接字服务器的语言)IE会自动 pthreads 使用多个内核吗?
  4. 如果我而不是线程怎么办?
  1. Is this accurate?
  2. Do I care? Each thread in this server will potentially run for several minutes and there will be lots of them. Is the OS (CentOS 6.5) smart enough to share the load?
  3. Is this any different from threading in C++? (language of the current socket server) IE do pthreads use multiple cores automatically?
  4. What if I fork instead of thread?

推荐答案

CRuby具有全局解释器锁定,因此它无法并行运行线程. Jruby和其他一些实现可以做到这一点,但是CRuby绝不会并行运行任何类型的代码.这意味着,无论您的操作系统多么智能,它都永远无法分担负载.

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

这与C ++中的线程不同. pthread创建真正的OS线程,内核的调度程序将同时在多个内核上运行它们.从技术上讲,Ruby也使用pthread,但是GIL阻止了它们并行运行.

This is different in threading in C++. pthreads create real OS threads, and the kernal's scheduler will run them on multiple cores at the same time. Technically Ruby uses pthreads as well, but the GIL prevents them from running in parallel.

Fork创建了一个新进程,并且您的OS的调度程序几乎肯定会足够聪明,可以在单独的内核上运行它.如果您需要Ruby中的并行性,请使用不带GIL的实现,或使用fork.

Fork creates a new process, and your OS's scheduler will almost certainly be smart enough to run it on a separate core. If you need parallelism in Ruby, either use an implementation without a GIL, or use fork.

这篇关于Ruby-如何跨内核/处理器进行线程化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆