将python + numpy代码嵌入到C ++ dll回调中 [英] Embedding python + numpy code into C++ dll callback

查看:460
本文介绍了将python + numpy代码嵌入到C ++ dll回调中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python嵌入的新用户。
我试图将python + numpy代码嵌入到C ++回调函数(在dll中)



我面临的问题是以下。如果我有:

  Py_Initialize(); 
//一些python glue
// python invocation
Py_Finalize();

一切正常。



我有:

  Py_Initialize(); 
_import_array(); //初始化numpy C-API
//一些python glue + numpy数组对象创建
//通过PyObject_CallObject()调用python
Py_Finalize();

这会在第二次到达_import_array (这意味着它适用于第一个回调)



如果我而是做python和numpy初始化只有一次,并在析构函数中完成(因此不是每次初始化/ ),



这里的问题我猜是numpy,但我不知道如何解决它

$ 。

$ div class =h2_lin>解决方案

尝试确保.dll只是初始化一次 b
$ b

以下是C ++ Singleton in DLL中的链接:



Singleton in a DLL?


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

解决方案

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?

这篇关于将python + numpy代码嵌入到C ++ dll回调中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆