1

I'm developing a little DLL as a proxy between a C++ application and Python scripts. I would like to exposed a function to restart the python interpreter. I have tried something like this:

    void initialize()
    {
        Py_Initialize();
    }

    void finalize()
    {
        Py_Finalize();
    }

I can initialize the interpreter and finalize it, but when I want to starting it again I am receiving the following error:

C:\A\31\s\Objects\structseq.c:398: bad argument to internal function

I have detected this happens when importing third-party modules (like numpy, pandas, tensorflow, etc) in Python code. I think this is due to some problem while freeing memory after the finalization, but not sure. The documentation says:

Bugs and caveats: The destruction of modules and objects in modules is done in random order; this may cause destructors (del() methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_FinalizeEx() more than once.

I have think to create a subinterpreter and use it to run my python scripts. When I want to restart the interpreter I could finalize the subinterpreter and create a new one. But I am not sure if this is the best way, especially considering threads.

  • I am faced with a similar issue, but can mitigate with using ""PyImport_ReloadModule" for my use case. I am just wondering you found any solution to this issue. Sub-interpreter and GIL dont go well together(Doc says somethin like .. "so note that combining this functionality with PyGILState_* APIs is delicate, because these APIs assume a".). Thanks – ecsridhar Nov 15 '21 at 22:58

0 Answers0