2012-04-18 32 views
2

我使用boost :: python的嵌入蟒蛇,这是我要做的事:Python嵌入并运行了多次

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

好听运行的第一次,但是当它被再次运行,我得到这个错误:

LookupError: unknown encoding: utf8

和代码不按预期运行,任何帮助表示赞赏。

+0

只需对此附加评论:从[boost文档](http://www.boost.org/doc/libs/1_52_0/libs/python/doc/tutorial/doc/html/python/embedding.html ),你现在不应该调用'Py_Finalize()'。他们不支持它。 – 2013-02-04 18:15:49

+0

@sharth你是正确的,我没有使用Py_Finalize调用它,但这不是一个理想的解决方案 – PLuS 2013-02-09 15:16:40

回答

2

既然你没有得到专家的答案,我提供我的学习工作在类似的问题。 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代表不同的执行状态(一种常见的解释器)

如果第二个对你的作品,它继续。