从C ++通过变量缓冲通过回调调用python [英] Calling python from C++, through callback, with variable buffer

查看:74
本文介绍了从C ++通过变量缓冲通过回调调用python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法编写了代码,以便可以通过回调从C ++调用python。

I managed to write code such that I can call python from C++ through a callback. ctypes was used.

Python中的回调设置如下:

The callback setup in Python is something like this:

proto = ctypes.CFUNCTYPE( ctypes.c_char_p, ctypes.POINTER( ctypes.c_char))

def parser( query, result):
   # Parsing code

fp = proto( parser_func)






这可以,但是我从C ++传递了一个固定的64k缓冲区。那是不可接受的:我需要处理一个可变缓冲区。我们提出的唯一解决方案是添加另一个回调,在该回调中,我们将句柄返回给python对象。然后,我们将其传递给解析器回调。然后也许我们需要第三个函数,以便Python可以回收内存,或者类似的东西


This works, but I am passing a fixed 64k buffer from C++. That won't be acceptable: I need to handle a variable buffer. The only solution we have come up with was to add another callback, in which we return a handle to a python object. We then pass that into the parser callback. And then maybe we need a third function such that Python can reclaim the memory, or something like that

如果有人有想法,寻找一种最简单的方法

Looking for a simplest method to do this if anyone has ideas

推荐答案

我最终实现了两个回调。一个计算长度(通过解析查询,然后返回长度。)另一个与第一个完全相同,但是通过客户端现在可以提供的缓冲区(知道长度)返回解析输出。它效率不高,但与我尝试过的其他所有方法都不一样。感谢您的建议

I ended up implementing two callbacks. One calculates the length ( by parsing the query, and returning the length.) The other parses exactly like the first, but returns the parse output via the buffer that the client can now supply, knowing the length. It's not efficient, but it worked unlike everything else I tried. Thanks for the suggestions

这篇关于从C ++通过变量缓冲通过回调调用python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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