使用boost::python从c++函数访问大型表
Large table access from c++ functions with boost::python
我正在c++中生成一个非常大的查找表,并在各种c++函数中使用它。使用boost::python将这些函数暴露给python。
当不作为类的一部分使用时,实现了期望的行为。当我尝试使用函数作为仅用python编写的类的一部分时,我遇到了访问查找表的问题。
------------------------------------
C++ for some numerical heavy lifting
------------------------------------
include <boost/python>
short really_big_array[133784630]
void fill_the_array(){
//Do some things which populate the array
}
int use_the_array(std::string thing){
//Lookup a few million things in the array depending on thing
//Return some int dependent on the values found
}
BOOST_PYTHON_MODULE(bigarray){
def("use_the_array", use_the_array)
def("fill_the_array", fill_the_array)
}
---------
PYTHON Working as desired
---------
import bigarray
if __name__ == "__main__"
bigarray.fill_the_array()
bigarray.use_the_array("Some Way")
bigarray.use_the_array("Some Other way")
#All works fine
---------
PYTHON Not working as desired
---------
import bigarray
class BigArrayThingDoer():
"""Class to do stuff which uses the big array,
use_the_array() in a few different ways
"""
def __init__(self,other_stuff):
bigarray.fill_the_array()
self.other_stuff = other_stuff
def use_one_way(thing):
return bigarray.use_the_array(thing)
if __name__ == "__main__"
big_array_thing_doer = BigArrayThingDoer()
big_array_thing_doer.use_one_way(thing)
#Segfaults or random data
我想我可能没有暴露足够的python来确保数组在正确的时间是可访问的,但我不太确定我应该暴露什么。同样有可能存在某种问题,涉及到查找表的所有权。
我不需要操作查找表,除非通过其他c++函数。
定义中缺少self。使用关键字参数,我可能应该使用关键字参数,所以运行时没有错误。
相关文章:
- 通过方法访问结构
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 使用不带参数的函数访问结构元素
- C++中高效的大型稀疏块压缩线性方程
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 用于访问容器<T>数据成员的正确 API
- 访问者访问变体并返回不同类型时出错
- 尝试通过多个向量访问变量时,向量下标超出范围
- 无法访问嵌套类.类的使用无效
- 在能够从 Web 浏览器访问大型数组数据的同时存储它的最可行方法是什么?
- 提高访问大型数组元素的性能
- 在大型C/C 项目中访问现有字段
- 设计一个具有大型成员的C++类,而这些成员很少在副本中访问
- C++:使用指针存储/访问大型对象以避免内存不足
- 高效地执行文件I/O,以便对大型三维网格进行非顺序访问
- 使用大型数组索引增加访问时间
- C++大型2D数组访问冲突
- 如何从具有userID和pageID的大型日志文件中找到最常访问的3个网页序列
- c++:访问大型数据集中的重成员变量的最有效和简洁的方法
- 使用boost::python从c++函数访问大型表