将有状态的 Python 脚本嵌入到 C++ 中使用 boost::python 的程序
我有一个不断生成数据的 C++ 程序。我有一个处理这些数据的 python 类。我想使用这个python类来处理数据:当每次生成数据点时,我可以使用这个python脚本来处理数据。但是这个Python脚本必须是“有状态的”,即它应该能够记住在这个数据点之前它做了什么。
一个超级基本的例子是,我的 C++ 程序只是生成数字,而我的 python 类计算生成的数字的累积和:
Python:
class CumSum:
def addone(x):
self._cumsum += x;
print self._cumsum;
C++
[Somehow construct a CumSum instance, say c]
for (int i=0; i<100000; i++) {
int x = rand() % 1000;
[Call c.addone(x)]
}
我听说 boost::python 是处理这个问题的好方法。谁能画出如何做的草图吗?我尝试阅读 boost 文档,但它们太大了,我无法消化。
我很感激你的帮助。
I have a C++ program that keeps generating data. I have a python class that process these data. I want to use this python class to process the data: when each time a data point is generated, I can use this python script to process the data. But this python script must be "stateful", i.e. it should be able to remember what it did before this data point.
One super basic example is, my C++ program just generates numbers, and my python class calculates the cumulative sums of the numbers generated:
Python:
class CumSum:
def addone(x):
self._cumsum += x;
print self._cumsum;
C++
[Somehow construct a CumSum instance, say c]
for (int i=0; i<100000; i++) {
int x = rand() % 1000;
[Call c.addone(x)]
}
I heard boost::python is a good way to handle this. Can anyone sketch out how to do it? I tried to read boost documents but they were too huge for me to digest.
I appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关如何执行 python 脚本的基本信息:
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/embedding.html
有关操作的详细信息C++ 中的 python 对象
http ://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/object.html
boost-python 的大部分内容都与将 C++ 类导出到 python 相关但你没有这样做,所以你可以忽略它。
您可能最好使用更简单的包装器,例如 SCXX
http://davidf.sjsoft。 com/mirrors/mcmillan-inc/scxx.html
For basic information about how to execute your python script:
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/embedding.html
For details on manipulating python objects in C++
http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/python/object.html
Much of boost-python is concerned with exporting your C++ classes to python but you aren't doing that so you can ignore it.
You may be better off using a simpler wrapper like SCXX
http://davidf.sjsoft.com/mirrors/mcmillan-inc/scxx.html