简体   繁体   English

在一个共享对象中增加python多个模块

[英]Boost python multiple modules in one shared object

I'm trying to create via boost python a package which will include several modules. 我正在尝试通过boost python创建一个包含几个模块的包。

The reasoning is that we want to expose a very large API, and it makes sense to group it in different modules for ease of use and to preserve python memory usage. 原因是我们希望公开一个非常大的API,将它组合在不同的模块中以便于使用和保留python内存使用是有意义的。 On the other hand we are forced (for reasons beyond the scope of this question to compile this into a single shared object ) 另一方面,我们被迫(由于超出此问题范围的原因将其编译为单个共享对象

So I create with boost python a package which exports several modules, as following: 所以我用boost python创建了一个导出几个模块的包,如下所示:

void exportClass1()
{
    namespace bp = boost::python;
    // map the IO namespace to a sub-module
    // make "from myPackage.class1 import <whatever>" work
    bp::object class1Module(bp::handle<>(bp::borrowed(PyImport_AddModule("myPackage.class1"))));
    // make "from mypackage import class1" work
    bp::scope().attr("class1") = class1Module;
    // set the current scope to the new sub-module
    bp::scope io_scope = class1Module;

    // export stuff in the class1 namespace

    class_<class1 >("class1", init<>())
    .
    .   CLASS SPECIFICS GO HERE
    .

    Other class of module class1 go here as well
}

BOOST_PYTHON_MODULE(myPackage)
{
    namespace bp = boost::python;

    // specify that this module is actually a package
    bp::object package = bp::scope();
    package.attr("__path__") = "myPackage";

    exportClass1();
    exportClass2();
    .
    .
    .

}

This code works. 这段代码有效。

The main problem is memory consumption. 主要问题是内存消耗。 The overall exposed api is very big so loading the entire package consumes roughly 65MB of ram, just for all the declarations. 整个暴露的api 非常大,所以加载整个包装消耗大约65MB的ram,仅用于所有声明。 (before the package user started doing anything) (在包用户开始做任何事情之前)

This is of course unacceptable. 这当然是不可接受的。 (given that loading a single module should consume maybe 1-3MB of ram) (假设加载单个模块应该消耗1-3MB的RAM)

When in python, if I call: 在python中,如果我打电话:

from myPackage.myModule import *

OR 要么

from myPackage.myModule import someClass

The memory consumption immidietly skyrockets to 65MB. 内存消耗量急剧上升至65MB。

After doing any of the imports if I call: sys.modules I see all the classes in my package as being "known" However if I run: 在执行任何导入后,如果我调用:sys.modules我看到我的包中的所有类都是“已知”但是如果我运行:

from myPackage.myModule import class1
c = class2()

I get an error: 我收到一个错误:

NameError: name 'class2' is not defined NameError:未定义名称“class2”

So it seems I get the worst of two worlds, on the one hands I consume memory as if I imported everything from my package, on the other hand I don't get the classes actually imported. 所以我似乎得到了两个世界中最糟糕的一方面,一方面我消耗内存,好像我从包中导入了所有东西,另一方面我没有得到实际导入的类。

Any ideas how to solve this, so that when I import a specific module only it will be imported, and not all the package data will be read to the python memory. 任何想法如何解决这个问题,这样当我导入一个特定的模块时,它只会被导入,而不是所有的包数据都会被读取到python内存中。 (which both takes time and consumes a lot of valuable memory) (这既花费时间又消耗了大量宝贵的记忆)

So this was much simpler than I assumed. 所以这比我想象的要简单得多。

The code above is correct also for making the calls in the form of: 以上代码也适用于以下列形式进行调用:

from myPackage.myModule import class1
c = class2()

What was preventing this from being executed correctly were the system paths. 阻止它正确执行的是系统路径。 The shared object was not placed in a location of the python path and did not have an __init__.py in the folder where it was placed. 共享对象未放置在python路径的位置,并且在放置它的文件夹中没有__init__.py

As soon as the shared object was placed in the correct site-packages folder, which of course has __init__.py the above example works correctly. 只要共享对象放在正确的site-packages文件夹中,当然有__init__.py ,上面的示例就可以正常工作。

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

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