2012-04-18 51 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代表不同的執行狀態(一種常見的解釋器)

如果第二個對你的作品,它繼續。