gpt4 book ai didi

c++ - 嵌入python并多次运行

转载 作者:太空狗 更新时间:2023-10-29 21:27:00 26 4
gpt4 key购买 nike

我正在使用 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代表不同的执行状态(一个共同的解释器)

如果第二个适合你,继续吧。

关于c++ - 嵌入python并多次运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10206833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com