简体   繁体   English

使用boost :: python从c ++函数进行大表访问

[英]Large table access from c++ functions with boost::python

I'm generating a very large lookup table in C++ and using it from a variety of C++ functions. 我正在C ++中生成一个非常大的查找表,并通过各种C ++函数使用它。 These functions are exposed to python using boost::python. 这些功能使用boost :: python公开给python。

When not used as part of a class the desired behaviour is achieved. 当不用作类的一部分时,可以实现所需的行为。 When I try and use the functions as part of a class written in just python I run in to issues accessing the lookup table. 当我尝试将这些功能用作仅用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

I think I am probably not exposing enough to python to make sure the array is accessible at the right times, but im not quite sure exactly what I shoudld be exposing. 我想我可能没有足够地使用python来确保在正确的时间可以访问该数组,但是我不确定我应该公开什么。 Equally likely that there is some sort of problem involving ownership of the lookup table. 同样可能存在某种涉及查找表所有权的问题。

I dont ever need to manipulate the lookup table except via the other c++ functions. 除了通过其他c ++函数之外,我不需要操纵查找表。

Missing a self in a definition. 在定义中缺少自我。 Used key word arguments where I should probably have used keyword only arguments so got no error when running. 使用了关键字参数,我应该只使用关键字参数,因此在运行时不会出错。

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

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