为什么django项目中的__init__模块在同一过程中加载了两次?

| 我正在尝试使用Boost.Python在高级python接口中包装c库。 c库的客户端合同之一是每个进程只能分配一次句柄。我希望可以通过使用全局模块在python方面强制执行此合同。 这是我的Django组件模块的
__init__.py
。 process1ѭ每个过程只能创建一次!
import my_c_mod
import os

print \"imager__init__()\"
print os.getpid()
ptl = my_c_mod.PyGenTL()
稍微相关的Boost.Python代码
    BOOST_PYTHON_MODULE(my_c_mod)
    {
        using namespace boost::python;

        // Create the Python type object for our extension class and define __init__ function.
        class_<PyGenTL>(\"PyGenTL\")
            .def(\"sys_info\", &PyGenTL::SysInfo)
            .def(\"list_cameras\", &PyGenTL::ListCameras)  // 
            .def(\"start_camera\", &PyGenTL::StartCamera)  // 
        ;
    }

    PyGenTL::PyGenTL()
    {
        try {
            std::cout << \"PyGenTL ctor(): allocating GenTL Lib.\" << std::endl;
            Detail::ThrowError(GCInitLib());
            Detail::ThrowError(TLOpen(&hTL));
        } catch (boost::exception& e) {
            std::cerr << \"PyGenTL ERROR! \";
            std::cerr << boost::diagnostic_information(e);
            std::cerr << std::endl;
        }
    }
注意构造函数中的打印语句,以及init中的os.getpid()。这是django进程的输出。请注意,在python的开头创建了两个进程,这就是为什么要创建两个
PyGenTL
的原因。到目前为止,一切都很好。
C:\\work\\svn\\sw\\branches\\python\\GenTlServer>python manage.py runserver
imager__init__()
2264
PyGenTL ctor(): allocating GenTL Lib.
imager__init__()
2912
PyGenTL ctor(): allocating GenTL Lib.
Validating models...

0 errors found
Django version 1.3, using settings \'GenTlServer.settings\'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
现在,在页面浏览期间,
__init__
在同一过程中被称为AGAIN(2912)
imager__init__()
2912
PyGenTL ctor(): allocating GenTL Lib.
ERROR (-1004): Requested module is in use.
PyGenTL ERROR! Requested module is in use.
[23/Jun/2011 18:02:22] \"GET / HTTP/1.1\" 500 76537
当然,可以通过在C端强制单例来解决我的特定问题,但是python的处理方式是什么?     
已邀请:
        因为它是通过
sys.path
中的两个不同条目加载的。对您的进口保持一致;我建议不执行项目就导入模块,例如
import <app>.<module>
。配置WSGI容器,使您不必依赖
manage.py
sys.path
修改将有所帮助。     

要回复问题请先登录注册