为什么Python在多处理或多线程应用程序中不比Java好? [英] Why Python is not better in multiprocessing or multithreading applications than Java?

查看:556
本文介绍了为什么Python在多处理或多线程应用程序中不比Java好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Python在GIL方面存在一些问题,因此Java更适合开发多处理应用程序。你可以用你的方式来证明java的有效处理的确切推理吗?

Since Python has some issues with GIL, Java is better for developing multiprocessing applications. Could you please justify the exact reasoning of java's effective processing than python in your way?

推荐答案

CPython中多线程的最大问题是全局解释器锁(GIL)(请注意其他Python实现必然会分享这个问题!)

The biggest problem in multithreading in CPython is the Global Interpreter Lock (GIL) (note that other Python implementations don't necessarily share this problem!)

GIL是一个实现细节,可以有效地阻止Python中单独线程的并行(同时)执行。问题是每当要执行Python字节代码时,当前线程必须已经获得了GIL,并且在任何给定时刻只有一个线程可以拥有GIL。

The GIL is an implementation detail that effectively prevents parallel (simultaneous) execution of separate threads in Python. The problem is that whenever Python byte code is to be executed, then the current thread must have acquired the GIL and only a single thread can have the GIL at any given moment.

因此,如果5个线程试图执行某些Python字节代码,那么它们将有效地运行交错,因为每个线程都必须等待GIL变为可用。这通常不是单核计算机的问题,因为物理约束具有相同的效果:一次只能运行一个线程。

So if 5 threads are trying to execute some Python byte code, then they will effectively run interleaved, because each one will have to wait for the GIL to become available. This is not usually a problem with single-core computers, as the physical constraints have the same effect: only a single thread can run at a time.

多核/ SMP计算机,但这成为一个瓶颈(而且每分钟变得越来越普遍)。

In multi-core/SMP computers, however this becomes a bottleneck (and those become more and more common every minute).

Java没有这样的限制,因此多个线程可以准确执行同一时间。

Java has no such restrictions, so multiple threads can execute at the exact same time.

这篇关于为什么Python在多处理或多线程应用程序中不比Java好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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