Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Python expansion Module acceleration Scheme

Shulou Source: shulou.com Published: 2022-06-03 15:19:52 10月04日 Update

This article mainly introduces the example analysis of the acceleration scheme of the Python extension module, which is very detailed and has a certain reference value. Interested friends must read it!

Ctypes (1)-first acquaintance

Many beginners to Python always say, "Python is a glue language."

But what on earth is glue language? I believe that many people do not know, or mistakenly think that glue is a language that can directly cooperate with all other languages.

Although Python has become very powerful today, the feature of "glue language" is rarely mentioned. But glue is still one of its most widely used properties. It's just that it has changed from being supplemented by Python to being dominated by Python now.

So what exactly are the "glue properties"?

In fact, Python has been closely related to the C language since it was born, and it can call the existing C _ engine + program without having to do the same thing that repeats the wheel. It saves a lot of cost of redevelopment. Many existing C programs can be managed and invoked with a Python as a hub, as if these C programs were "stuck with glue". This is the source of glue properties.

With the development of the Python language, Python has become powerful and robust enough, and the glue feature has been gradually weakened in the process of optimizing performance, because Python has become so powerful that it does not need to rely on the C language. But this feature has been retained. So, now that Python is powerful enough, what else do you need this feature to do?

In the final analysis, Python is an interpreted dynamic scripting language, coupled with GIL locks that have not been solved for more than a decade, both execution efficiency and concurrency are the biggest shortcomings of Python.

Therefore, the existence of this feature gives today's Pythoner a new choice. Those modules that have high requirements for execution efficiency and concurrency are implemented in C. Using C # to help Python solve performance problems in some special cases has become the most common solution in the Python community today. Many people do not know that, in fact, many of our commonly used libraries take advantage of this feature.

Data compatibility layer

Since the feature of Python is a large family, let's first take a look at a compatibility layer module made between Python and C-ctypes module.

Before we can understand the ctypes module, we need to understand which data types ctypes provides for compatibility:

Note: the following table is listed according to the Python3 version. The only difference is between the native string type and the long integer.

Try to share a library. What is a shared library?

In fact, the C language does not have to generate executable files after compilation, but can exist in the form of a "shared library" that can be shared to other programs, and other programs can take advantage of this existing C-compiled program by calling this shared library.

In order to help the novice friends more directly, we take the Windows system as an example and call the VC shared library. Under Windows, the VC shared library is suffixed with the dll extension by default.

We show it by loading the cdll module that cdecl calls the rule function.

First of all, we have to import cdll. Please refer to the statement.

From ctypes import cdll

Then the connection to the msvcrt.dll shared library file can be obtained directly by accessing the properties of cdll.

Libc = cdll.msvcrt

Then, we can get the corresponding VC function from this shared library, the most common is the printf function (ps: I wonder if it has caused some old drivers' tears? )

Printf = libc.printf

Now that we've got a function in a shared library, let's give it a try.

Let's first try to run it in IDLE.

Nothing happened! Seems to have been deeply deceived! Don't worry, it's one of the reasons for freedom.

Let's first take a look at the result of the printf function call, using print to see

It seems that the return value of the printf function is the number of bytes output, but we still have a problem to solve, that is, where is the output?

In fact, the printf function is a C function and will not be output to Python, so you need to use the console to output.

The above is all the content of the article "sample Analysis of Python extension Module acceleration Scheme". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Features languages glue functions modules programs power output solutions that is examples analysis content performance efficiency data novice articles types questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Shulou Technology Docker MariaDB