Python, Ruby, Haskell - Do they provide true multithreading?

后端 未结 8 645
我寻月下人不归
我寻月下人不归 2021-02-01 22:31

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?

相关标签:
8条回答
  • 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.

    0 讨论(0)
  • 2021-02-01 23:04

    Haskell is suitable for anything. python has processing module, which (I think - not sure) helps to avoid GIL problems. (so it suitable for anything too).

    But my opinion - best way you can do is to select highest level possible language with static type system for big and huge things. Today this languages are: ocaml, haskell, erlang.

    If you want to develop small thing - python is good. But when things become bigger - all python benefits are eaten by miriads of tests.

    I didn't use ruby. I still thinking that ruby is a toy language. (Or at least there's no reason to teach ruby when you know python - better to read SICP book).

    0 讨论(0)
提交回复
热议问题