编程语言
首页 > 编程语言> > c-嵌入python并运行多次

c-嵌入python并运行多次

作者:互联网

我正在使用boost :: python嵌入python,这是我的方法:

void runCode(){
    Py_Initialize();
    //boost::python code goes here and embedded python code runs
    Py_Finalize();
}

第一次运行良好,但是再次运行时,出现此错误:

LookupError: unknown encoding: utf8

并且代码未按预期运行,请多多帮助.

解决方法:

由于您没有得到专家的答案,因此我提供了解决类似问题的经验.
Python的reinitialization support存在问题.如果由于某些错误而需要重新启动解释器,或者想要运行许多独立的解释器,这是很不幸的.

问题之一是资源和内存泄漏(来自以上链接):

Bugs and caveats: 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_Finalize() more than once.

另一个问题是许多模块不正确地支持此功能,例如in this SO thread.我认为这是您面临的问题.

似乎大多数Python应用程序都可以解决此问题:

>使发动机在专用的程序中运行;
>通过使用subinterpreters来表示(公共解释器的)不同的执行状态

如果第二个适合您,请继续进行.

标签:boost-python,python,c-4
来源: https://codeday.me/bug/20191201/2080907.html