简体   繁体   中英

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. These functions are exposed to python using boost::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.

    ------------------------------------
    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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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