困惑,像python、ruby这样的语言是单线程的吗?不像说java?(对于网络应用程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3086467/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:18:42  来源:igfitidea点击:

Confused, are languages like python, ruby single threaded? unlike say java? (for web apps)

javapythonrubymultithreading

提问by Blankman

I was reading how Clojure is 'cool' because of its syntax + it runs on the JVM so it is multithreaded etc. etc.

我正在阅读 Clojure 是如何“酷”的,因为它的语法 + 它在 JVM 上运行,因此它是多线程的等等。

Are languages like ruby and python single threaded in nature then? (when running as a web app).

那么像 ruby​​ 和 python 这样的语言本质上是单线程的吗?(作为网络应用程序运行时)。

What are the underlying differences between python/ruby and say java running on tomcat?

python/ruby 和在 tomcat 上运行的 java 之间的根本区别是什么?

Doesn't the web server have a pool of threads to work with in all cases?

Web 服务器不是在所有情况下都有一个线程池可以使用吗?

采纳答案by J?rg W Mittag

Both Python and Ruby have full support for multi-threading. There are some implementations (e.g. CPython, MRI, YARV) which cannot actually run threads in parallel, but that's a limitation of those specific implementations, not the language. This is similar to Java, where there are also some implementations which cannot run threads in parallel, but that doesn't mean that Java is single-threaded.

Python 和 Ruby 都完全支持多线程。有一些实现(例如 CPython、MRI、YARV)实际上不能并行运行线程,但这是这些特定实现的限制,而不是语言的限制。这类似于 Java,其中也有一些实现不能并行运行线程,但这并不意味着 Java 是单线程的。

Note that in both cases there are lots of implementations which canrun threads in parallel: PyPy, IronPython, Jython, IronRuby and JRuby are only few of the examples.

请注意,在这两种情况下,都有许多可以并行运行线程的实现:PyPy、IronPython、Jython、IronRuby 和 JRuby 只是其中的几个例子。

The main difference between Clojure on the one side and Python, Ruby, Java, C#, C++, C, PHP and pretty much every other mainstream and not-so-mainstream language on the other side is that Clojure has a saneconcurrency model. All the other languages use threads, which we have known to be a bad concurrency model for at least 40 years. Clojure OTOH has a sane update model which allows it to not only present one but actually multiple sane concurrency models to the programmer: atomic updates, software transactional memory, asynchronous agents, concurrency-aware thread-local global variables, futures, promises, dataflow concurrency and in the future possibly even more.

一方面,Clojure 与 Python、Ruby、Java、C#、C++、C、PHP 以及几乎所有其他主流和非主流语言之间的主要区别在于,Clojure 具有健全的并发模型。所有其他语言都使用线程,至少 40 年来,我们都知道这是一种糟糕的并发模型。Clojure OTOH 有一个健全的更新模型,它允许它不仅向程序员展示一个而且实际上是多个健全的并发模型:原子更新、软件事务内存、异步代理、并发感知线程局部全局变量、期货、承诺、数据流并发并且在未来可能更多。

回答by BalusC

Certainly the webserver will have a pool of threads. That's only outside the control of your program. Those threads are used to handle HTTP requests. Each HTTP request is handled in a separate thread and the thread is released back to pool when the associated HTTP response is finished. If the webserver doesn't have such a pool, it would have been extremely slow in serving.

当然,网络服务器会有一个线程池。这只是在您的程序控制之外。这些线程用于处理 HTTP 请求。每个 HTTP 请求都在一个单独的线程中处理,当相关的 HTTP 响应完成时,该线程被释放回池。如果网络服务器没有这样的池,它的服务会非常缓慢。

Whether a programming language is singlethreaded or multithreaded dependens on the possibility to programmatically spawn newthreads using the language in question. If that isn't possible, then the language is singlethreaded, for example PHP. As far as I can see, both Ruby and Python supports multithreading.

编程语言是单线程还是多线程取决于使用相关语言以编程方式生成线程的可能性。如果这是不可能的,那么语言是单线程的,例如 PHP。据我所知,Ruby 和 Python 都支持多线程。

回答by ablerman

Most languages don't define single or multithreading. Usually, that is left up to the libraries to implement.

大多数语言没有定义单线程或多线程。通常,这由库来实现。

That being said, some languages are better at it than others. CPython, for instance, has issues with interpreter locking during multithreading, Jython (python running on the JVM) does not.

话虽如此,有些语言比其他语言更擅长。例如,CPython 在多线程期间存在解释器锁定问题,而 Jython(在 JVM 上运行的 Python)则没有。

Some of the real power of Clojure (IMO) is that it runs on the JVM. You get multithreading and tons of libraries for free.

Clojure (IMO) 的一些真正强大之处在于它在 JVM 上运行。您可以免费获得多线程和大量库。

回答by Jesse Wolgamott

Ruby

红宝石

The Ruby Interpreter is single threaded, which is to say that several of its methods are not thread safe.

Ruby 解释器是单线程的,也就是说它的几个方法不是线程安全的。

In the Rails world, this single-thread has mostly been pushed to the server. So, you'll see nginx running with a pool of mongrel servers, each of which has an interpreter in memory, processes 1 request at a time, and in its own thread.

在 Rails 世界中,这种单线程大多被推送到服务器。因此,您将看到 nginx 与一组混合服务器一起运行,每个服务器在内存中都有一个解释器,一次处理 1 个请求,并且在自己的线程中。

Passenger, running "ruby enterprise"brings the concept of garbage collection and some thread safety into Rails, and it's nice.

乘客,运行“ruby 企业”将垃圾收集的概念和一些线程安全带入 Rails,这很好。

Still work to be done in Rails on this area, but it's getting there slowly -- but in general, the idea is to have multiple services and servers.

Rails 在这方面仍有工作要做,但进展缓慢——但总的来说,这个想法是拥有多个服务和服务器。

回答by Peter Tillemans

How to untangle the knots in al those threads...

如何解开所有这些线中的结...

Clojure did not invent threading, however it has particularly strong support for it with Software Transactional Memory, Atoms, Agents, parallel map operations, ...

Clojure 并没有发明线程,但是它在软件事务内存、原子、代理、并行映射操作、......

All other have accumulated threading support. Ruby is a special case as it has green threads in some implementations which are a kind of software emulated threads and do not use all the cores. 1.9 will put this to rest.

所有其他都积累了线程支持。Ruby 是一个特例,因为它在某些实现中具有绿色线程,这些线程是一种软件模拟线程并且不使用所有内核。1.9 将使其停止。

Regarding web servers, no they do not always work multithreaded, apache has traditionally ran as a flock of daemons which are a pool of separate single threaded processes. Now currently there are more options to run apache servers.

关于 web 服务器,不,它们并不总是多线程工作,apache 传统上作为一群守护进程运行,这些守护进程是一个单独的单线程进程池。现在目前有更多选项可以运行 apache 服务器。

To summarize all modern languages support threading in one form or another.

总而言之,所有现代语言都以一种或另一种形式支持线程。

The newer languages like scala and clojure are adding specific support to improve working with multiple threads without explicit locking as this has traditionally be the great pitfall of multithreading.

像 scala 和 clojure 这样的新语言正在添加特定的支持,以改进在没有显式锁定的情况下使用多线程,因为这在传统上是多线程的一大陷阱。

回答by Ben Hughes

The short answer is yes, they are single threaded.

简短的回答是肯定的,它们是单线程的。

The long answer is it depends.

答案很长,这取决于。

JRuby is multithreaded and can be run in tomcat like other java code. MRI (default ruby) and Python both have a GIL (Global Interpreter Lock) and are thus single threaded.

JRuby 是多线程的,可以像其他 Java 代码一样在 tomcat 中运行。MRI(默认 ruby​​)和 Python 都有 GIL(全局解释器锁),因此是单线程的。

The way it works for web servers is further complicated by the number of available server configurations. For most ruby applications there are (at least) two levels of servers, a proxy/static file server like nginx and then the ruby app server.

可用服务器配置的数量使 Web 服务器的工作方式更加复杂。对于大多数 ruby​​ 应用程序,有(至少)两个级别的服务器,一个代理/静态文件服务器,如 nginx,然后是 ruby​​ 应用程序服务器。

Nginx does not use threads like apache or tomcat, it uses non-blocking events (and I think forked worker processes). This allows it to deal with higher levels of concurrency than would be allowed with the overhead and scheduling inefficiencies of native threads.

Nginx 不使用像 apache 或 tomcat 这样的线程,它使用非阻塞事件(我认为是分叉的工作进程)。这使它能够处理比本地线程的开销和调度效率低下所允许的更高级别的并发性。

The various ruby apps servers also work in different ways to get high throughput and concurrency without threads. Thin uses libev and the asynchronous evented model like Nginx. Mongrel uses a round-robin pool of worker processes. Unicorn uses native Unix IPC (select on a socket) to load balance to a pool of forked processes through one master proxy socket.

各种 ruby​​ 应用程序服务器也以不同的方式工作,以在没有线程的情况下获得高吞吐量和并发性。Thin 使用 libev 和像 Nginx 这样的异步事件模型。Mongrel 使用工作进程的循环池。Unicorn 使用原生 Unix IPC(在套接字上选择)通过一个主代理套接字将负载平衡到一个分叉进程池。

Threads are only one way to address concurrency. Multiple processes and evented models are a different approach that ties in well with the Unix base. This is fundamentally different from the way Java treats the world.

线程只是解决并发问题的一种方式。多进程和事件模型是一种与 Unix 基础很好地结合的不同方法。这与 Java 对待世界的方式有着根本的不同。

回答by James Schek

CPython has a Global Interpreter Lockwhich can reduce the performance of multi-threaded code in Python. The net effect, in some cases, is that threads can't actually run simultaneously because of locking contention. Not all Python implementations use a GIL so this may not apply to JPython, IronPython or other implementations.

CPython 有一个全局解释器锁,它可以降低 Python 中多线程代码的性能。在某些情况下,最终的结果是,由于锁定争用,线程实际上无法同时运行。并非所有 Python 实现都使用 GIL,因此这可能不适用于 JPython、IronPython 或其他实现。

The language itself does support threading and other asynchronous operations. The python libraries can also support threading internally without exposing it directly to the Python interpreter.

该语言本身确实支持线程和其他异步操作。python 库还可以在内部支持线程,而无需将其直接暴露给 Python 解释器。

If you've heard anything negative about Python and threading (or that it doesn't support it), it is probably someone encountering a situation where the GIL is causing a bottleneck..

如果你听说过关于 Python 和线程的任何负面消息(或者它不支持它),那么很可能是有人遇到了 GIL 导致瓶颈的情况。

回答by tkr

A few interpreted programming languages such as CPython and Ruby support threading, but have a limitation that is known as a Global Interpreter Lock (GIL). The GIL is a mutual exclusion lock held by the interpreter that prevents the interpreter from concurrently interpreting the applications code on two or more threads at the same time, which effectively limits the concurrency on multiple core systems.

一些解释型编程语言(如 CPython 和 Ruby)支持线程,但有一个限制,即全局解释器锁 (GIL)。GIL是解释器持有的互斥锁,防止解释器同时在两个或多个线程上并发解释应用程序代码,有效限制了多核系统上的并发。

from wikipedia Thread

维基百科主题

回答by Tanel Suurhans

Reading these answers here... A lot of them try to sound smarter than they really are imho (im mostly talking about Ruby related stuff as thats the one i'm most familiar with). In fact, JRuby is currently the only Ruby implementation that supports true concurrency. On JVM Ruby threads are mapped to OS native threads, without GIL interfering. So its totally correct to say that Ruby is not multithreaded. In 1.8.x Ruby is actually run inside one OS thread, and while you do have the fake feeling of concurrency with green threads, then in reality GIL will pretty much prevent you from having true concurrency. In Ruby 1.9 this changed a bit, as now a Ruby process can have many OS threads attached to it (plus the green threads), but again GIL will totally destroy the point and become the bottleneck.

在这里阅读这些答案......他们中的很多人试图听起来比他们实际上更聪明,恕我直言(我主要谈论与Ruby相关的东西,因为那是我最熟悉的东西)。事实上,JRuby 是目前唯一支持真正并发的 Ruby 实现。在 JVM 上,Ruby 线程被映射到 OS 本地线程,而不会受到 GIL 的干扰。所以说 Ruby 不是多线程是完全正确的。在 1.8.x 中,Ruby 实际上是在一个 OS 线程中运行的,虽然你确实有绿色线程并发的假感觉,但实际上 GIL 会阻止你拥有真正的并发。在 Ruby 1.9 中,这一点有所改变,因为现在一个 Ruby 进程可以有许多操作系统线程附加到它(加上绿色线程),但是 GIL 将再次完全破坏这一点并成为瓶颈。

In practice, from a regular webapp standpoint, it should not matter much if its single or multithreaded. The problem mostly arises on the server side anyhow and it mostly is a matter of scaling technique difference.

实际上,从常规 web 应用程序的角度来看,它是单线程还是多线程应该无关紧要。无论如何,问题主要出现在服务器端,并且主要是缩放技术差异的问题。

回答by Daniel Rodriguez MSFT

Yes Ruby and Python can handle multi-threading, but for many cases (web) is better to rely on the threads generated by the http requests from the client to the server. Even if you generate many threads on a same application to low the runtime cost or to handle many task at time, in a web application case that's usually too much time, no one will wait happily more than some fractions of a second for the response of your application in a single page, it's more wise to use AJAX (Asynchronous JavaScript And XML) techniques: make sure the design of your web shows up rapidly, and make an asynchronous insertion of those hard-coding things later.

是的,Ruby 和 Python 可以处理多线程,但在许多情况下(Web)最好依赖于从客户端到服务器的 http 请求生成的线程。即使您在同一个应用程序上生成许多线程以降低运行时成本或同时处理许多任务,在通常时间过长的 Web 应用程序情况下,没有人会高兴地等待几分之一秒的响应您的应用程序在单个页面中,使用 AJAX(异步 JavaScript 和 XML)技术更明智:确保您的 Web 设计快速显示,然后异步插入那些硬编码的东西。

That does not mean that multi-threading is useless for web! It's highly recommended to low the charge of your server if you want to run recursive-complicated-hardcore-applications (not for a website, I mean), but what that thing return must end in files or in databases, so then could be softly served by a http response.

这并不意味着多线程对 Web 毫无用处!如果您想运行递归复杂的核心应用程序(不是针对网站,我的意思是),强烈建议降低服务器的费用,但是返回的内容必须以文件或数据库结尾,因此可以轻柔由 http 响应提供服务。