Why isn't Python better in multi-processor or multi-threaded applications than Java?

Since Python has some problems with GIL, Java is better at developing multiprocessor applications. Could you justify the exact argument for efficient java handling than python is in your way?

+7
source share
3 answers

The biggest multithreading issue in CPython is Global Interpreter Lock (GIL) (note that other Python implementations do not have to share this issue!)

GIL is an implementation detail that effectively prevents the parallel execution of individual threads in Python. The problem is that whenever a Python bytecode is to be executed, the current thread must receive a GIL, and only one thread can have a GIL at any time.

So, if 5 threads are trying to execute some Python bytecode, then they will effectively start interleaving, because each of them will have to wait for the GIL to appear. This is usually not a problem with single-core computers, since physical limitations have the same effect: only one thread can work at a time.

In multi-core / SMP computers, however, this becomes a bottleneck (and all this is becoming more and more common).

Java does not have such restrictions, so multiple threads can run at the same time.

+16
source

I think you answered your question. Python has problems due to the Global Interpreter Lock (GIL).

First Google hit for "python gil": http://www.grouplens.org/node/244

0
source

I would not agree that Python is no better than Java for a multiprocessor application.

First, I guess the OP uses “better” to mean “faster code execution,” as far as I can tell.

Am I suffering from ugliness-speed syndrome, perhaps because I came from the C / ASM background, so I spent a lot of time getting to the end of “slow Python”? question.

A simple answer to this question? "May be." Here are a few important points:

1) When using a multi-threaded application, Python will have a drawback for any language that has nothing like GIL. GIL is a Python VM artifact in CPython, not a Python language. Some Python VMs, such as Jython, IronPython, etc., do not have a GIL.

2) The Multi-Process application doesn’t really use the GIL application, and now you can start executing your Python code more quickly without being subjected to more load from the GIL. I highly recommend that if you want to write large Python code that requires both speed and concurrency, you will learn about Multi-Processing and possibly ZMQ / 0MQ for messaging.

3) Regardless of GIL, Java displays faster code execution than Python in many areas. This is due to fundamental differences in the way Python handles objects in memory:

  • Several Python functions create copies of objects in memory, but do not modify them (see http://www.skymind.com/~ocrow/python_string/ for an example)

  • Python uses a dict to store attributes for objects, etc. I don’t want to be distracted and delve into these areas, but I can say that some of the “neat” things that Python can do are speed. It is also important to know that there are ways associated with the default behavior, if this leads to too high a speed for you.

4) Some of the benefits of Java speed are related to more optimization in the Java VM over Python, as far as I can tell. Once you resolve the differences in how much backstage memory / object work is done, Java can often still beat Python. Is this because Java has more focus than Python? I'm not sure, with enough funding, I feel that CPython can be faster.

I will say that I decided to accept Python almost 100%, promoting new code.

Do not fall into the trap of premature optimization and remember that you can always call C code as a last resort. Make your code workable, make it serviceable, and then start optimizing as soon as the application speed is not fast enough for your needs.

Interesting benchmarks:

http://benchmarksgame.alioth.debian.org/u64/python.php

More information on Python performance issues can be found here:

http://www.infoworld.com/d/application-development/van-rossum-python-not-too-slow-188715

0
source

All Articles