簡體   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