Python vs. Java——你会选择哪个进行并发编程,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1861457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python vs. Java -- Which would you pick to do concurrent programming and why?
提问by Setzer
Also, if not python or java, then would you more generally pick a statically-typed language or a dynamic-type language?
另外,如果不是 python 或 java,那么你会更普遍地选择静态类型语言还是动态类型语言?
回答by johnstok
I would choose the JVM over python, primarily because multi-threading in Python is impeded by the Global Interpreter Lock. However, Java is unlikely to be your best when running on the JVM. Clojureor Scala(using actors) are both likely to be better suited to multi-threaded problems.
我会选择 JVM 而不是 Python,主要是因为 Python 中的多线程受到Global Interpreter Lock 的阻碍。但是,在 JVM 上运行时,Java 不太可能是最好的。Clojure或Scala(使用actor)都可能更适合多线程问题。
If you do choose Java you should consider making use of the java.util.concurrentlibraries and avoid multi-threading primitives such as synchronized.
如果您确实选择 Java,则应考虑使用java.util.concurrent库并避免使用诸如synchronized 之类的多线程原语。
回答by halfdan
Definetely Stackless Python! That a Python variant especially made for concurrency.
绝对无堆栈的 Python!这是一个专为并发而设计的 Python 变体。
But in the end it depends on your target platform and what you are trying to achieve.
但最终这取决于您的目标平台以及您要实现的目标。
回答by Ben James
For concurrency, I would use Java. By use Java, I actually mean Scala, which borrows a lot from Erlang's concurrency constructs, but is (probably) more accessible to a Java developer who has never used either before.
对于并发,我会使用 Java。通过使用 Java,我实际上是指Scala,它从 Erlang 的并发构造中借用了很多,但对于以前从未使用过的 Java 开发人员来说(可能)更容易使用。
Python threads suffer from having to wait for the Global Interpreter Lock, making true concurrency (within a single process) unachievable for CPU-bound programs. As I understand, Stackless Python solves some (though not all) of CPython's concurrency deficiencies, but as I have not used it, I can't really advise on it.
Python 线程不得不等待全局解释器锁,这使得 CPU 密集型程序无法实现真正的并发(在单个进程内)。据我了解,Stackless Python 解决了 CPython 的一些(虽然不是全部)并发性缺陷,但由于我没有使用过它,我无法真正就此提出建议。
回答by blank
I don't think the argument is about language choice or static or dynamic typing - it's between two models of concurrency - shared memory and message passing. Which model makes more sense in your situation & does your chosen language allow you to make a choice or are you forced to adopt one model over the other?
我不认为争论是关于语言选择或静态或动态类型——它是在两种并发模型之间——共享内存和消息传递。哪种模型在您的情况下更有意义?您选择的语言是否允许您做出选择,或者您是否被迫采用一种模型而不是另一种模型?
Why not have a look at Erlang(which has dynamic typing) and message passing, the Actor model, and read why Joe Armstrong doesn't like shared memory. There's also a interesting discussion about java concurrency using locks and threads here on SO.
为什么不看看Erlang(它具有动态类型)和消息传递、Actor 模型,并阅读Joe Armstrong 不喜欢共享内存的原因。在 SO上还有一个关于使用锁和线程的 Java 并发性的有趣讨论。
I don't know about Python, but Java, along with the inbuilt locks and threads model, has a mesasge passing framework called Kilim.
我不了解 Python,但 Java 连同内置的锁和线程模型,都有一个名为Kilim的消息传递框架。
回答by Peter
If not Java/Python I would go for a functional languagesince taking side effects into account is one of the complexities of writing concurrent software. (As far as your question goes : this one happens to be statical typed, but compiler infered most of the time)
如果不是 Java/Python,我会选择函数式语言,因为考虑副作用是编写并发软件的复杂性之一。(就您的问题而言:这个恰好是静态类型的,但编译器大部分时间都是推断出来的)
Personally I would pick F#, since I've seen lots of nice examples of writing concurrent software with ease using it.
就我个人而言,我会选择 F#,因为我已经看到很多使用它编写并发软件的好例子。
As an introduction : this man is equally fun as inspiring, even a must have seen if you are not interested in F# what so ever.
作为介绍:这个人既有趣又鼓舞人心,如果您对 F# 不感兴趣,即使是他也必须看到。
回答by e-satis
I would use Java, via Jython. Java has strong thread capabilities, and it can be written using the Python syntax with Jython, so you got the best of the two worlds.
我会通过 Jython 使用 Java。Java 具有强大的线程功能,并且可以使用 Python 语法和 Jython 编写它,因此您可以两全其美。
Python itself is not really good with concurrency, and is slower than Java anyway.
Python 本身在并发方面并不是很好,而且无论如何都比 Java 慢。
But if you have concurrency issues and free hands, I'd have a look at Erlang because it has been design for such problems. Of course, you must consider Erlang only if you have:
但是如果你有并发问题和空闲的手,我会看看 Erlang,因为它是为这些问题而设计的。当然,只有在您有以下情况时才必须考虑 Erlang:
- time to master a (very) new technology
- control on a reasonable part of the production chain, since Erland need some adaptations in your toolbox to fit
- 掌握(非常)新技术的时间
- 控制生产链的合理部分,因为 Erland 需要在您的工具箱中进行一些调整以适应
回答by Matthew Sowders
Neither. Concurrent programming is notoriously hard to get correct. There is the option of using a process oriented programming language like occam-piwhich is based of the idea of communicating sequential processesand the pi calculus. This allows compile time checking for deadlock and many other problems that arise during concurrent systems development. If you do not like occam-pi, which I cant blame you if you dont, you could try Gothe new language from google which also implements a version of CSP.
两者都不。众所周知,并发编程很难正确。可以选择使用面向过程的编程语言,如occam-pi,它基于通信顺序过程和pi 演算的思想。这允许编译时检查死锁和并发系统开发期间出现的许多其他问题。如果您不喜欢 occam-pi(如果您不喜欢我不能责怪您),您可以尝试Go来自 google 的新语言,它也实现了 CSP 的一个版本。
回答by Jamie McCrindle
The answer is that it depends. For example are you trying to take advantage of multiple cores or cpus on a single machine or are you wanting to distribute your task across many machines? How important is speed vs. ease of implementation?
答案是视情况而定。例如,您是想在一台机器上利用多个内核或 CPU,还是想将您的任务分配到多台机器上?速度与实施的简易性有多重要?
As mentioned before, Python has the Global Interpreter Lock but you could use the multiprocessingmodule. Note that while Stacklessis very cool, it won't utilise multiple coreson its own. Python is usually considered easier to work with than Java. If speed is a priority Java is usually faster.
如前所述,Python 具有全局解释器锁,但您可以使用多处理模块。请注意,虽然Stackless非常酷,但它不会单独使用多个内核。Python 通常被认为比 Java 更容易使用。如果优先考虑速度,Java 通常更快。
The java.util.concurrentlibrary in Java makes writing concurrent applications on a single machine simpler but you'll still need to synchronise around any shared state. While Java isn't necessarily the best language for concurrency, there are a lot of tools, libraries, documentation and best practices out there to help.
java.util.concurrentJava 中的库使在单台机器上编写并发应用程序变得更简单,但您仍然需要围绕任何共享状态进行同步。虽然 Java 不一定是最好的并发语言,但有很多工具、库、文档和最佳实践可以提供帮助。
Using message passing and immutability instead of threads and shared state is considered the better approach to programming concurrent applications. Functional languages that discourage mutability and side effects are often preferred as a result. If distributing your concurrent applications across multiple machines is a requirement, it is worth looking at runtimes designed for this e.g. Erlangor Scala Actors.
使用消息传递和不变性而不是线程和共享状态被认为是编写并发应用程序的更好方法。因此,不鼓励可变性和副作用的函数式语言通常是首选。如果需要在多台机器上分发并发应用程序,那么值得查看为此设计的运行时,例如Erlang或Scala Actors。
回答by Yin Zhu
For some tasks, Python is too slow. Your single thread Java program could be faster than the concurrent version of Python on a multi-core computer...
对于某些任务,Python 太慢了。您的单线程 Java 程序可能比多核计算机上的 Python 并发版本更快……
I'd like to use Java or Scala, F# or simply go to C++(MPI and OpenMPI).
我想使用 Java 或 Scala、F# 或简单地使用 C++(MPI 和 OpenMPI)。
回答by Dave Kirby
The Java environment (JVM + libraries) is better for concurrency than (C)Python, but Java the language sucks. I would probably go with another language on the JVM - Jython has already been mentioned, and Clojure and Scala both have excellent support for concurrency.
Java 环境(JVM + 库)比 (C)Python 更适合并发,但 Java 语言很糟糕。我可能会在 JVM 上使用另一种语言——Jython 已经被提到过,Clojure 和 Scala 都对并发有很好的支持。
Clojure is particularly good - it has support for high performance persistent data structures, agents and software transactional memory. It is a dynamic language but you can give it type hints to get performance as good as Java.
Clojure 特别好——它支持高性能持久数据结构、代理和软件事务内存。它是一种动态语言,但您可以给它类型提示以获得与 Java 一样好的性能。
Watch this videoon InfoQ by Richard Hickey (creator of Clojure) on the problems with traditional approaches to concurrency, and how Clojure handles it.
观看此视频与传统方法并发和Clojure中如何处理它的问题相关的理查德·希基(Clojure的创建者)。

