I am working on Python version 2.7.
I have a module extension for Python written in C.
The module initialization function PyMODINIT_FUNC initmymodule
contains some code for initializing OpenSSL library. My module built as shared library and loading via imp.load_dynamic
This module may loading many times and I can't control it. Django and python doing that. And when it loading twice then OPENSSL_config
function calling twice too. And it leading to process crash.
I can't control it from C-code, I can't control it from Python-code.
Here look at the docs http://docs.python.org/2.7/library/imp.html It says:
imp.load_dynamic Load and initialize a module implemented as a dynamically loadable shared library and return its module object. If the module was already initialized, it will be initialized again.
Nice.
I found that the similar problem was solved in Python version 3.4 http://hg.python.org/cpython/file/ad51ed93377c/Python/import.c#l459
Modules which do support multiple initialization set their m_size field to a non-negative number (indicating the size of the module-specific state). They are still recorded in the extensions dictionary, to avoid loading shared libraries twice.
But what shall I do in Python 2.7?
Maybe to do workaround by registering own custom import hook where you could control the case which causes you problem (prevent double initialization). Some references for writing custom import hooks:
This is hackish solution, so I suggest extra caution if this is to be used in production systems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With