Python - C 嵌入式分段错误
问题描述
我面临类似于 Py_initialize/Py_Finalize 的问题不能用 numpy 工作两次 .. C 中的基本编码:
I am facing a problem similar to the Py_initialize / Py_Finalize not working twice with numpy .. The basic coding in C:
Py_Initialize();
import_array();
//Call a python function which imports numpy as a module
//Py_Finalize()
程序处于循环中,如果 python 代码将 numpy 作为导入的模块之一,它会给出段错误.如果我删除 numpy,它工作正常.
The program is in a loop and it gives a seg fault if the python code has numpy as one of the imported module. If I remove numpy, it works fine.
作为临时解决方法,我尝试不使用 Py_Finalize(),但这会导致巨大的内存泄漏 [随着 TOP 的内存使用量不断增加而观察到].我试过但不明白我发布的那个链接中的建议.有人可以建议在导入诸如 numpy 的同时完成通话的最佳方法.
As a temporary work around I tried not to use Py_Finalize(), but that is causing huge memory leaks [ observed as the memory usage from TOP keeps on increasing ]. And I tried but did not understand the suggestion in that link I posted. Can someone please suggest the best way to finalize the call while having imports such as numpy.
谢谢桑托什
解决方案
我不太确定您似乎不理解 Py_initialize/Py_Finalize 不能与 numpy 一起工作两次.发布的解决方案非常简单:每次程序执行时只调用 Py_Initialize 和 Py_Finalize 一次.不要在每次运行循环时都调用它们.
I'm not quite sure how you don't seem to understand the solution posted in Py_initialize / Py_Finalize not working twice with numpy. The solution posted is quite simple: call Py_Initialize and Py_Finalize only once for each time your program executes. Do not call them every time you run the loop.
我假设您的程序在启动时会运行一些初始化命令(仅运行一次).在那里调用 Py_Initialize.永远不要再调用它.另外,我假设当你的程序终止时,它有一些代码可以删除东西、转储日志文件等.在那里调用 Py_Finalize.Py_Initialize 和 Py_Finalize 并非旨在帮助您管理 Python 解释器中的内存.不要为此使用它们,因为它们会导致您的程序崩溃.相反,请使用 Python 自己的函数来删除您不想保留的对象.
I assume that your program, when it starts, runs some initialization commands (which are only run once). Call Py_Initialize there. Never call it again. Also, I assume that when your program terminates, it has some code to tear down things, dump log files, etc. Call Py_Finalize there. Py_Initialize and Py_Finalize are not intended to help you manage memory in the Python interpreter. Do not use them for that, as they cause your program to crash. Instead, use Python's own functions to get rid of objects you don't want to keep.
如果您确实必须在每次运行代码时创建一个新环境,您可以使用 Py_NewInterpreter 并创建一个子解释器,然后使用 Py_EndInterpreter 来销毁该子解释器.它们记录在 Python C API 页面底部附近.这类似于拥有一个新的解释器,除了每次启动子解释器时不会重新初始化模块.
If you really MUST create a new environment every time you run your code, you can use Py_NewInterpreter and to create a sub-interpreter and Py_EndInterpreter to destroy that sub-interpreter later. They're documented near the bottom of the Python C API page. This works similarly to having a new interpreter, except that modules are not re-initialized each time a sub-interpreter starts.
相关文章