6

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

Johan Råde
  • 20,480
  • 21
  • 73
  • 110
Pa_
  • 641
  • 1
  • 5
  • 17
  • If you can fully control how the DLL is built, I would peronally leave the details to distutils and swig. Here is an example project (interfacing with numpy): https://github.com/martinxyz/python/tree/master/realistic – maxy Sep 24 '11 at 18:33

1 Answers1

2

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?

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Hi Paul, thanks for your answer! So, i am pretty sure my dll is initialized only once, and in fact, i omitted to say that if i also initialize python at the initialization of my dll, and finalize in the destructor, something like this works fine: `PyRun_SimpleString(` `"from time import time,ctime\n"` `"from numpy import *\n"` `"print 'Today is',ctime(time())\n"` `);` but if i try to call python via PyObject_CallObject(), then it does not work anymore, and it crashes when the callback terminate – Pa_ Sep 24 '11 at 18:15
  • 1
    Hi - 1) Make sure you initialize Python only ONCE during program execution (and deinitialize no MORE than once, otherwise never). 2) Look at the martinxyz sample code posted above, 3) Look at this thread - a similar problem, caused by a reference count error: http://www.velocityreviews.com/forums/t566237-pyobject_callobject-code-dump-after-calling-4-times.html – paulsm4 Sep 24 '11 at 19:27
  • Thanks a lot Paul, this link helped a lot! in fact the problem was a DECREF of an object used by PyTuple_SetItem(). Now it seems to work, but i have to do some more extensive tests, of course! Thanks again! – Pa_ Sep 24 '11 at 19:41
  • 1
    aewsome I did this `if (!Py_IsInitialized()){ ... initialize python interpreter ...` } – imbr Sep 09 '19 at 13:09