问题
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