Sometimes the Python program needs to be extended using the C program. Under what circumstances should I use a C program to expand Python? In my superficial understanding, there are at least the following scenarios:
1. efficiency problem: although Python is very efficient in programming, its execution efficiency is also good, but some of the key to performance still need to be implemented using C code, then call the C program in Python, so the performance may be higher.
2. make full use of the excellent C function library: some excellent function libraries in the C language world (such as the libcurl mentioned above), which have powerful functions and excellent performance, if you can simply package these C function libraries, you can easily call them in Python.
According to the official guidance documents, I wrote a jay C extension module, which contains a method "hello (used to greet a person ^_^). A C file is required to implement the function of this module, A setup is required. compile, install, and deploy the py script module. (Is setup. py very familiar? This file is included in the source code of almost every Python library ...) Source code can be obtained directly at: https://github.com/smilejay/python/tree/master/py2013.
1. The content of the program file jaymodule. C is as follows: (read the reference document at the end of this article. I am also worried that I cannot explain it clearly)
The code is as follows: |
Copy code |
# Include <Python. h> Static PyObject * jay_hello (PyObject * self, PyObject * args ){ Const char * person; Int sts; If (! PyArg_ParseTuple (args, "s", & person )) Return NULL; Printf ("Hello, % s. I'm Jay. n", person ); Return Py_None; /* Sts = printf ("Hello, % s. I'm Jay. n", person ); Return Py_BuildValue ("I", sts ); */ } Static PyMethodDef JayMethods [] = { {"Hello", jay_hello, METH_VARARGS, "Say hello to a person from Jay ."}, {NULL, NULL, 0, NULL}/* Sentinel */ }; PyMODINIT_FUNC initjay (void ){ PyImport_AddModule ("jay "); (Void) Py_InitModule ("jay", JayMethods ); } Int main (int argc, char * argv []) { /* Pass argv [0] to the Python interpreter */ Py_SetProgramName (argv [0]); /* Initialize the Python interpreter. Required .*/ Py_Initialize (); /* Add a static module Py_Exit (0 ); } |
2. The Python script for setup. py to build and install the module is as follows:
The code is as follows: |
Copy code |
From distutils. core import setup, Extension Module1 = Extension ('Jay ', Sources = ['jaymodule. C']) Setup (name = 'Jay ', Version = '1. 0 ', Description = 'This is a demo package from Jay .', Ext_modules = [module1]) |
3. The construction and installation method is simple, that is, the traditional "sudo python setup. py install ":
The code is as follows: |
Copy code |
Jay @ jay-linux :~ /Workspace/python. git/py2013 $ sudo python setup. py install Running install Running build Running build_ext Running install_lib Copying build/lib. Linux-x86_64-2.7/jay. so->/usr/local/lib/python2.7/dist-packages Running install_egg_info Removing/usr/local/lib/python2.7/dist-packages/jay-1.0.egg-info Writing/usr/local/lib/python2.7/dist-packages/jay-1.0.egg-info |
4. Demonstrate the effect of jay in Python as follows:
The code is as follows: |
Copy code |
Jay @ jay-linux :~ $ Python Python 2.7.3 (default, Sep 26 2013, 20:03:06) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> Import jay >>> Jay. hello ('Tom ') Hello, Tom. I'm Jay. >>> |
The demonstration here is to allow my C module to be dynamically loaded in Python; if you have a module that uses this library as the built-in, load the Python interpreter directly, the compilation and installation methods are a little different. You need to add your extension modules to the source code of the Python interpreter and re-compile the Python interpreter. Refer to the official documentation for compilation-and-linkage.
Of course, here is the use of Python2.7 for demonstration, Python3 C extension is similar (slightly different), if you want to learn more about Python3 writing, please refer to: http://docs.python.org/3/extending/extending.html