3

I'm using boost::python to embed python, this is how I do it:

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

it runs nicely for the first time, but when it is run again, I get this error:

LookupError: unknown encoding: utf8

and code does not run as expected, any help is appreciated.

2
  • 1
    Just an additional comment on this: From the boost documentation, you currently should never call Py_Finalize(). They don't support it. Commented Feb 4, 2013 at 18:15
  • @sharth You are correct and I'm calling it without using Py_Finalize but this is not a desired solution Commented Feb 9, 2013 at 15:16

1 Answer 1

3

Since you didn't get an expert answer, I'm offering my learning from working on a similar problem. Python have issues with reinitialization support. This is unfortunate if you need to restart the interpreter due to some error, or want to run many independent interpreters.

One issue there is leaking resources and memory (quoting from the above link):

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.

Another issue is many modules don't support this properly, as can be seen for example in this SO thread. I think this is the problem you're facing.

It seems that most Python applications work-around this problem:

  • by having the engine run in a dedicated process ;
  • by using subinterpreters which represent distinct execution states (of a common interpreter)

If the second one works for you, go ahead with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.