Can I solve the problem of using multiple cores in Python?

Source: Internet
Author: User
Today I read an article about the most difficult problem with Python. Speaking of Python, it is limited by GIL. Do not use multiple threads. Use multiple processes. Q: Will I solve the problem of using multiple Python cores in the future? Are there the same problems with other interpreted languages, such as Ruby? What is the current solution? Today I read an article about the most difficult Python issue. Speaking of Python is limited by GIL. Do not use multiple threads. Use multiple processes.

Q:
  1. Will the exploitation of multiple Python cores be solved in the future?
  2. Are there the same problems with other interpreted languages, such as Ruby?
  3. What is the current solution?
Reply: Ruby also has GIL. In fact, GIL is not the root cause of performance problems, but GC is the root cause of performance problems.

Assuming that GIL is removed, multi-core and multi-threading like Java, you will face more headaches such as OOM and GC. A Full GC in Java is stopped whole world, you don't want the entire server to stop with eight kernels. Are you waiting for the GC to complete?

Therefore, Java has invested so many years to improve the GC efficiency of the VM and develop various GC algorithms, especially parallel GC algorithms. To improve GC efficiency and avoid overhead of memory heap scanning on large memory servers, Java also emphasizes running multiple processes on a single machine.

So do you think Python/Ruby solves the problem by removing GIL? In fact, more and more troublesome problems will be introduced.

If you use coroutine to improve the I/O concurrency performance, Ruby now supports coroutine native, and Lua is a script language with high efficiency in coroutine, which has proved this path.

Finally speaking of the multi-core issue, Ruby's future development direction is MVM, that is, multiple VMS are run in a single process, each CPU core runs one VM, and each VM has its own GC. This method is actually very good. It not only effectively utilizes the process shared memory, but also supports multi-core parallel processing, and solves the global GC performance problem.
  1. For a long time, the official implementation of CPython in Python will not solve the GIL problem.
  2. This problem also exists in Ruby's official implementation of MRI.
  3. There are many solutions. The most common one is to use multi-process. If conditions permit, you can also choose implementation without GIL restrictions, such as JVM-based Jython and JRuby.
Of course, Python can use multiple cores.

The biggest misunderstanding about Python is probably "Python's MultithreadingMulti-core cannot be used ".

Taking server development, one of the mainstream Python applications, as an example, it is very common to develop a number of processes based on the CPU core.

For scenarios where "multithreading" is required (such as Asynchronous), there are also many "coroutine" solutions that can be used, such as stackless python, greenlet, and the cute gevent developed from this. First, the level at which the language should support multithreading. C supports multithreading through the preemptive schedtive of the operating system, and provides synchronization mechanisms such as critical section and wait/notify. The problem is that such a synchronization mechanism is too low-level and often needs to be encapsulated as an advanced synchronization mechanism in practical applications, such as a multi-threaded producer-consumer queue. Designers of advanced dynamic languages have three options:
  1. Provides a mechanism similar to C (Java) in a language );
  2. A well-designed C interface is used to run multiple virtual machines in the same process. C is used to encapsulate the low-level synchronization mechanism into a high-level synchronization mechanism (Lua) between multiple virtual machines ), or, you can encapsulate multi-threaded Optimization in an API (such as Intel MKL's various multi-core algorithms ).
  3. Multi-process.
Method 1 is not worth the candle. Because the advantage of low-level synchronization mechanisms lies in efficiency, efficiency must be considered at the underlying level, such as L1/L2/L3 cache effectiveness. This is beyond the control of the language for interpreting execution (even with JIT. Therefore, the underlying synchronization mechanism provided by advanced dynamic languages is not necessary. Use Methods 2 and 3. 2 is the most flexible.

Is it difficult to remove GIL? Not hard, Because there is no need to remove it.. If Python can initialize multiple VMS in a process, and its standard library makes enough common multi-core optimizations at the C level, there is no problem. Python actually wants to implement another function, asynchronous operations. Although asynchronous operations and parallel computing can both be done through multiple threads, the former is more suitable for the completion of coroutine or user-level threads. However, Python is a stackful implementation, that is, byte code uses the C runtime stack to maintain its own running status. The weakness of this mechanism is that it is not easy to use cross-platform methods to implement coroutine. Therefore, using OS multithreading and GIL becomes a compromise means to simulate coroutine semantics.

In addition. Although C's thread and thread synchronization mechanisms are at the underlying level, only such mechanisms can cover all use cases. Other high-level abstractions can only be used in certain cases. Therefore, when I say that advanced dynamic languages require advanced thread operations, I implicitly mean that these languages also require good interoperability with C to expand this abstraction at any time.

Recommended reading

| Python thread, GIL, and ctypes

This article starts from:

1. python does not need to remove GIL, which reduces the running efficiency.

2. Use multi-process parallel code

3. Use ctypes to bypass GIL and implement multi-thread Parallel Processing

Explained GIL

Agree with Liang Zhi's answer.

If there are multiple cores, it should be the pursuit of high concurrency and high throughput.
At this time, python is recommended.

Of course, there are always some amazing technologies in this world.

For example, the operating system written in C.
SharpOS (operating system)

It takes a lot of effort to implement other technologies that can be easily done.
It is not a matter of pleasure and praise.

Tools are used to solve problems, not to create problems. 1. There are no plans for the time being, mainly because it has a great impact on the existing GC and is difficult to implement.
2. Similar to Rio's answer, but I don't know any other language.
3,
The impact of GIL is not as big as imagined, for example, for io-intensive (Network/disk) programs.

Pypy previously provided an idea to remove GIL (based on STM ):
Http://morepypy.blogspot.jp/2011/06/global-interpreter-lock-or-how-to-kill.html
However, GIL is not removed in pypy2.0.

In addition to multi-process, using coroutine (stackless python) is also a good solution, but I have not done any performance tests ..... Who told you that CPython cannot be used to directly support multithreading is bullshit, and Java/C # is supported from the first version, because it is a good question. Due to GIL restrictions, cPython cannot simply make full use of the parallel processing capabilities of multi-core processors. However, using multiprocessing and other multi-process methods will consume additional overhead for inter-process communication, it cannot achieve the efficiency of multithreading.

On the other hand, it seems that the current bottleneck of python in the high concurrency of large data volumes is not very obvious. python is usually used to process lightweight tasks. When I use python to process T-level natural corpus in my work, it also relies on the mapreduce platform. Therefore, it is not a very obvious problem to avoid multithreading in big data processing.

In addition, the current GIL mechanism involves resetting the core of cPython from the underlying layer, including resource synchronization and gc policies, it will greatly reduce the performance of python in processing simple single-threaded tasks, because even a single thread needs to synchronize resources, the overhead is quite large. Therefore, cPython will not change its current GIL in the foreseeable future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.