Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding python + numpy code into C++ dll callback

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

like image 753
Pa_ Avatar asked Sep 24 '11 17:09

Pa_


1 Answers

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?

like image 191
paulsm4 Avatar answered Oct 05 '22 11:10

paulsm4