In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how C API references counters in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Brief introduction
The memory management of Python is realized through the reference counter of the object, the creation of the object will increase the reference counter by 1, and the reference counter will be increased by 1 each time it is referenced, otherwise, the reference counter will be subtracted by 1 when the reference counter of the Python object is 0, the object will be recovered and released.
This method of memory management has some disadvantages, one is that it needs extra space to maintain reference counting, and the other is that it can not solve the problem of "circular reference" of objects. Therefore, there are many languages such as Java that do not use this algorithm to do garbage collection mechanism.
Python code example
Import sysdef test_refcount (a): print ("func a refcount: {}" .format (sys.getrefcount (a)) if _ _ name__ = ='_ _ main__': / / directly create the Python object a = 189987319 print ("a refcount: {}" .format (sys.getrefcount (a) / / call the Python object an once Then add 1 b = a print ("b, a refcount: {}" .format (sys.getrefcount (a) / / to the list, field, or tuple, the reference counter will add 1 c = [a] print ("c, a refcount: {}" .format (sys.getrefcount (a) / / when using the function call, add 1 when passing parameters When called, the reference counter is also added by 1, so it is added by 2 test_refcount (a) the result: a refcount: 1b, a refcount: 2C, a refcount: 3func a refcount: 5Python C API manages and releases Python objects
Void Py_INCREF (PyObject * o) Python object reference counter plus 1, this object cannot be NULL, otherwise an error will be reported.
Void Py_XINCREF (PyObject * o) Python object reference counter plus 1, the object can be NULL, but the reference counter is not in effect
The reference counter of the void Py_DECREF (PyObject * o) Python object is minus 1. The object cannot be NULL, otherwise an error will be reported.
Void Py_XDECREF (PyObject * o) Python object reference counter minus 1, the object can be NULL, but the reference counter is not in effect
Void Py_CLEAR (PyObject * o) directly clears the Python application counter 0
C code example
Header file
/ Created by lanyulei on 18-9-9.//#ifndef PRINT_DEMO1_PYREFCOUNT_H#define PRINT_DEMO1_PYREFCOUNT_H#include # include void pyRefCount (); # endif / / PRINT_DEMO1_PYREFCOUNT_H
Source file
/ Created by lanyulei on 18-9-9.//#include "pyRefCount.h" / / retention and release of Python objects void pyRefCount () {PyObject* py_ival = Py_BuildValue ("I", 56486); / / create object printf ("Py_BuildValue: py_ival refcount:% ld\ n", Py_REFCNT (py_ival)); / / print reference counter Py_XINCREF (py_ival) for Python objects / / the reference counter of the Python object plus 1 printf ("Py_BuildValue: py_ival refcount:% ld\ n", Py_REFCNT (py_ival)); / / prints the reference counter of the Python object Py_XDECREF (py_ival); / / the reference counter of the Python object minus 1 printf ("Py_BuildValue: py_ival refcount:% ld\ n", Py_REFCNT (py_ival)) / / print the reference counter of the Python object Py_CLEAR (py_ival); / / clear the reference counter of the Python object 0 printf ("Py_BuildValue: py_ival refcount:% ld\ n", Py_REFCNT (py_ival)); / / print the reference counter of the Python object}
Main.cpp
# include "pyRefCount.h" int main () {/ / initialize the Python virtual machine Py_Initialize (); / / determine whether the Python virtual machine is successful if (Py_IsInitialized () = = 0) {printf ("fal to initialize Python\ n"); return-1;} printf ("server start\ n"); pyRefCount (); / exit Python virtual machine Py_Finalize (); return 0 } initialize the Python script path
When the Python virtual machine is running, when import a module, if the module has not been loaded, the Python virtual machine will search the script in the path where the program is executed. If the script file is found, it will be loaded. If it is not found, it will be searched from all the paths in the sys.path. If it is found, the script file will be loaded, otherwise, an error will be reported.
Sys.path under Python
Print all paths under sys.path
> import sys > sys.path [','C:\\ WINDOWS\\ SYSTEM32\\ python27.zip','D:\\ software\\ Python2\\ DLLs','D:\\ software\\ Python2\\ lib','D:\\ software\\ Python2\\ lib\\ plat-win', 'D:\\ software\\ Python2\\ lib\\ lib-tk','D:\\ software\\ Python2','D:\\ software\\ Python2\\ lib\\ site-packages' 'd:\\ software\\ Python2\\ Lib\\ site-packages\\ pytesser_v0.0.1']
Add the current path to the sys.path
> > import os > > import sys > > sys.path.append (os.getcwd ()) > > sys.path [','C:\\ WINDOWS\\ SYSTEM32\\ python27.zip', 'D:\\ software\\ Python2\\ DLLs', 'D:\\ software\\ Python2\\ lib', 'D:\\ software\\ Python2\\ lib\\ plat-win', 'D:\ software\\ Python2\\ lib\\ lib-tk', 'D:\\ software\\ Python2' 'D:\\ software\\ Python2\\ lib\\ site-packages','D:\\ software\\ Python2\\ Lib\\ site-packages\\ pytesser_v0.0.1','C:\\ WINDOWS\\ system32']
Python script path initialized by Candlespace +
Header file
/ Created by win7 on 2018/9/10.//#ifndef INC_20180910_PYINIT_H#define INC_20180910_PYINIT_H#include # include bool initPython (); # endif / / INC_20180910_PYINIT_H
Source file
/ Created by win7 on 2018/9/10.//#include "pyInit.h" bool initPython () {char scriptPath; sprintf (scriptPath, "sys.path.append ('% s')", ". / script"); / / register our script path PyRun_SimpleString ("import sys") with Python; PyRun_SimpleString (scriptPath); PyRun_SimpleString ("print sys.path"); return true;} Thank you for reading! This is the end of the article on "how to quote counters in C API in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.