Embedding python + numpy code into C++ dll callbac

2019-06-21 05:51发布

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条回答
闹够了就滚
2楼-- · 2019-06-21 06:27

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?

查看更多
登录 后发表回答