繁体   English   中英

使用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 ++函数之外,我不需要操纵查找表。

在定义中缺少自我。 使用了关键字参数,我应该只使用关键字参数,因此在运行时不会出错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM