A detailed introduction to Python and C calling each other

Source: Internet
Author: User
Tags sendmsg
While Python development is highly efficient, it is not very performance as a scripting language, so in order to take into account development efficiency and performance, it is common to implement high performance modules in C or C + + or run Python scripts in C or C + + to handle logic, which is usually the way some of the modules in Python are implemented. The latter is more common in server-side programs (for business expansion or plugin functionality) and game development (scripts only handle logic). This article focuses on the implementation of Python and C by running Python scripts in C, and the use of the same area of memory by C and Python scripts as an example.

Objective

Recently because of the needs of the work, in the consideration of UDP based on a data synchronization protocol for online games, in order to pre-test data, decided to do an external proxy tunnel, the principle is in the server side and client side set up a network forwarding proxy, that is, the original C/ s connection changed to two proxies for fast data transfer. Because the UDP library is written in C + + code, in the test data need to constantly modify parameters, recompile, modify the output statistics, such as tabulation, patience, finally decided to export the interface by the Python script to make a logical call. The following words do not say, come together to see the detailed introduction:

Preparatory work

In order to run a Python script in C, you need to link the Python virtual Library to the program link, the Python virtual library is the Python27.lib file in the Python installation directory libs, and the library link into the program can be Google. Because some python methods and data structures are used in C, the include directory under the Python installation directory needs to be added to the project include directory. Well, that's all you need to prepare, and then you can start implementing an example of setting up a memory area.

There are a number of ways to export to Python, and the following can be different depending on your needs:

1, ctypes binding. cTYPES is included in the Universal Python standard library module, which can be loaded into a dynamic-link library (DLL,SO) at runtime and supported on CPython 2.x/3.x and PyPy. The advantage of this way is not to use the Python API to write the export function, you can directly load the dynamic link library symbol table, in Python can be directly called.

2, the third party's python binding. An example is Boost-python, which is implemented by tool automation using the PYTHON/C API to generate a series of C + + wrapper functions. Ideal for large libraries or engines exported to Python.

3. Write Python binding function manually. If you are familiar with the Python C API, this approach should be the most flexible, read the API documentation can be used. Efficiency should be the best in theory, but it may take some time for me to be a beginner of python.

With the previous toss of the C function exported to the Lua script experience, I thought we should first study Python C API, and then make half to get it done. The ctypes of the Python standard library module was later found to be very powerful, although the performance should be the worst in three ways, but in this maximum 60fps tunnel, the loss of the C/python interface boundary call is ignored first. Unlike the other two ways of design, cTYPES uses a non-invasive method of calling interfaces, do not need to modify the original C interface or write some binding code, directly to the compiled dynamic library to call. cTYPES is also very enjoyable to use.

Below is a description of the use of the following ctypes:

1. Load DLL Dynamic link library

It is important to note that the dynamic link library function uses cdecl or stdcall calling conventions to load dynamic libraries using Cdll or Windll, respectively.

For example:

# Load UDP library function udp_server = Cdll. LoadLibrary ("./udp_server.so") Init_udp_server = udp_server.init_udp_server Destroy_udp_server = Udp_server.destroy_ Udp_server Update_udp_server = udp_server.update_udp_server sendmsg = udp_server. sendmsg setconnectcallback = Udp_server. Setconnectcallback setdisconnectcallback = Udp_server. Setdisconnectcallback settimeoutcallback = Udp_server. Settimeoutcallback setrecvcallback = Udp_server. Setrecvcallback

2. Data type Mapping

In addition to the basic data types defined by cTYPES (C_char, C_int, c_double, etc.), you can also use pointer functions to convert to pointer types. For the network library to be exported, it is necessary to set the callback function, in the C + + library, the callback function is done by setting a function pointer, and cTYPES also supports the declaration of the function pointer. For example recv_cb = CFUNCTYPE( None, c_char_p, c_int ) , represents a callback function with a return value of void and a parameter of type char* and int.


Def init (self, port, ip= "127.0.0.1"):   self._port = port   self._ip = IP   self._clients = {}   self.c_connect _CB = CONNECT_CB (self.server_connect)   SELF.C_DISCONNECT_CB = DISCONNECT_CB (self.server_disconnect)   self.c_ TIMEOUT_CB = TIMEOUT_CB (self.server_timeout)   SELF.C_RECV_CB = RECV_CB (SELF.SERVER_RECV) def create (self):   if Self._port:    If Init_udp_server (self._ip, self._port) = = 0:     print "Server Listen%s:%d"% (self._ip, Self._port) C10/>setconnectcallback (SELF.C_CONNECT_CB)     setdisconnectcallback (SELF.C_DISCONNECT_CB)     Settimeoutcallback (SELF.C_TIMEOUT_CB)     setrecvcallback (SELF.C_RECV_CB)     return True   print "[ERROR] Init_udp_server error ", SELF._IP, Self._port   return False

The binding callback parameter needs to be noted that the bound callback function needs to be saved as a member variable (above) in order to avoid Python garbage collection causing the callback function to become a wild pointer. This is a small hole. Basically, a small library uses these features as well.

Related Article

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.