Embedding python + numpy code into C++ dll callback

孤人 提交于 2019-12-23 09:40:26

问题


I am new of python embedding. I am trying to embed python + numpy code inside a C++ callback function (inside a dll)

the problem i am facing is the following. if i have:

Py_Initialize();
// some python glue
// python invocation
Py_Finalize();

everything works fine.

but if i have:

Py_Initialize();
_import_array(); //to initialize numpy C-API
// some python glue + numpy array object creation
// python invocation via PyObject_CallObject()
Py_Finalize();

this crashes at the second time it reaches _import_array(); (meaning that it works for the first callback)

if i instead do the python and numpy initialization just once and the finalization in the destructor (thus not every time initializing/finalizing), everything crashes when leaving the callback..

The problem here i guess is numpy, but i dont know how to solve it


回答1:


Try make sure your .dll is only initialized once, regardless of how many times the code is actually invoked.

Here is a link on "C++ Singleton in a DLL":

Singleton in a DLL?



来源:https://stackoverflow.com/questions/7540808/embedding-python-numpy-code-into-c-dll-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!