Python,Ruby,Haskell - 他们提供真正的多线程吗? [英] Python, Ruby, Haskell - Do they provide true multithreading?

查看:120
本文介绍了Python,Ruby,Haskell - 他们提供真正的多线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们计划在任何一种非常高级的编程语言中编写一个高度并发的应用程序。



1)Python,Ruby或Haskell支持真正的多线程?



2)如果程序包含线程,虚拟机会自动将工作分配给多个内核(如果主板上有多个CPU,则将工作分配给物理CPU)?



真多线程 =多个独立执行线程利用多个内核提供的资源(而非只有一个内核)。



假多线程 =线程模拟多线程环境,而不依赖任何本机操作系统功能。

解决方案


1)Python,Ruby或Haskell是否支持真多线程?


这与语言无关。这是一个硬件问题(如果机器只有1个CPU,它在物理上不可能同时执行两个指令),操作系统(再次,如果操作系统不支持真正的多线程,没有什么



除非语言规范明确禁止或强制执行真正的多线程,这绝对没有什么与语言相关的。 / p>

全部您提及的语言,以及目前为止在答案中提及的所有语言,有多种实现方式,其中一些支持真正的多线程,有些没有,有些是建立在其他执行引擎之上,可能或可能不支持真正的多线程。



Ruby,例如。




  • MRI:绿色线程,没有真正的多线程

    / li>
  • YARV:操作系统线程,没有真正的多线程

  • Rubinius:操作系统线程,真正的多线程

  • MacRuby:OS线程,真多线程

  • JRuby,XRuby:JVM线程,依赖于JVM(如果JVM支持真多线程,那么JRuby / XRuby也是, 可以做的事情)

  • IronRuby,Ruby.NET:就像JRuby,XRuby,但是在CLI而不是在JVM上



另请参见我对另一个类似的关于Ruby的问题。 (注意,答案是一年多,而且有些不再准确。例如,Rubinius现在使用真正的并发本机线程,而不是真正并发的绿线程。此外,从那时起,几个新的



类似于Python:

$ b

类似于Python的实现已经出现了,比如BlueRuby,tinyrb,Ruby Go Lightly,Red Sun和SmallRuby。
$ b

  • CPython:本机线程,没有真正的多线程

  • PyPy:本机线程,取决于执行引擎,或者在JVM之上,或者在CLI之上,或者在另一个Python执行引擎之上)。每当底层平台支持真多线程时,PyPy也会这么做。)

  • Unladen Swallow:本机线程,目前没有真正的多线程,但计划修复

  • Jython:JVM线程,请参阅JRuby

  • IronPython:CLI线程,参见IronRuby



对于Haskell,至少Glorious Glasgow Haskell编译器支持使用本机线程的真多线程。我不知道UHC,LHC,JHC,YHC,HUGS或所有其他人。



对于Erlang,真正的多线程与绿线程。


2)如果程序包含线程,虚拟机将自动将工作分配给多个核心


再次:这取决于虚拟机,操作系统和硬件。此外,上面提到的一些实现甚至都没有虚拟机。


We are planning to write a highly concurrent application in any of the Very-High Level programming languages.

1) Do Python, Ruby, or Haskell support true multithreading?

2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there is more than 1 CPU on the mainboard)?

True multithreading = multiple independent threads of execution utilize the resources provided by multiple cores (not by only 1 core).

False multithreading = threads emulate multithreaded environments without relying on any native OS capabilities.

解决方案

1) Do Python, Ruby, or Haskell support true multithreading?

This has nothing to do with the language. It is a question of the hardware (if the machine only has 1 CPU, it is simply physically impossible to execute two instructions at the same time), the Operating System (again, if the OS doesn't support true multithreading, there is nothing you can do) and the language implementation / execution engine.

Unless the language specification explicitly forbids or enforces true multithreading, this has absolutely nothing whatsoever to do with the language.

All the languages that you mention, plus all the languages that have been mentioned in the answers so far, have multiple implementations, some of which support true multithreading, some don't, and some are built on top of other execution engines which might or might not support true multithreading.

Take Ruby, for example. Here are just some of its implementations and their threading models:

  • MRI: green threads, no true multithreading
  • YARV: OS threads, no true multithreading
  • Rubinius: OS threads, true multithreading
  • MacRuby: OS threads, true multithreading
  • JRuby, XRuby: JVM threads, depends on the JVM (if the JVM supports true multithreading, then JRuby/XRuby does, too, if the JVM doesn't, then there's nothing they can do about it)
  • IronRuby, Ruby.NET: just like JRuby, XRuby, but on the CLI instead of on the JVM

See also my answer to another similar question about Ruby. (Note that that answer is more than a year old, and some of it is no longer accurate. Rubinius, for example, uses truly concurrent native threads now, instead of truly concurrent green threads. Also, since then, several new Ruby implementations have emerged, such as BlueRuby, tinyrb, Ruby Go Lightly, Red Sun and SmallRuby.)

Similar for Python:

  • CPython: native threads, no true multithreading
  • PyPy: native threads, depends on the execution engine (PyPy can run natively, or on top of a JVM, or on top of a CLI, or on top of another Python execution engine. Whenever the underlying platform supports true multithreading, PyPy does, too.)
  • Unladen Swallow: native threads, currently no true multithreading, but fix is planned
  • Jython: JVM threads, see JRuby
  • IronPython: CLI threads, see IronRuby

For Haskell, at least the Glorious Glasgow Haskell Compiler supports true multithreading with native threads. I don't know about UHC, LHC, JHC, YHC, HUGS or all the others.

For Erlang, both BEAM and HiPE support true multithreading with green threads.

2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there is more than 1 CPU on the mainboard)?

Again: this depends on the Virtual Machine, the Operating System and the hardware. Also, some of the implementations mentioned above, don't even have Virtual Machines.

这篇关于Python,Ruby,Haskell - 他们提供真正的多线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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