Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Python is not better in multiprocessing or multithreading applications than 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?

like image 405
Nava Avatar asked Jun 22 '11 11:06

Nava


People also ask

Why Python is not good for multithreading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.

Is Python good for multiprocessing?

If your code is IO bound, both multiprocessing and multithreading in Python will work for you. Multiprocessing is a easier to just drop in than threading but has a higher memory overhead.

Why is multithreading slower Python?

This is due to the Python GIL being the bottleneck preventing threads from running completely concurrently. The best possible CPU utilisation can be achieved by making use of the ProcessPoolExecutor or Process modules which circumvents the GIL and make code run more concurrently.

Which programming language is best for multithreading?

C/C++ Languages Now Include Multithreading Libraries Programming languages, such as C and C++, have evolved to make it easier to use multiple threads and handle this complexity. Both C and C++ now include threading libraries.


1 Answers

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

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.

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.

In multi-core/SMP computers, however this becomes a bottleneck. These days almost everthing is running on multiple cores, including effectively all smartphones and even many embedded systems.

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

like image 52
Joachim Sauer Avatar answered Oct 03 '22 00:10

Joachim Sauer