title: Multiple independent embedded Python Interpreters on multiple operating system threads invoked from C/C++ program
body: <p>Embedding Python interpreter in a <a href="http://docs.python.org/extending/embedding.html" rel="noreferrer">C/C++ application is well documented</a>. What is the best approach to run multiple python interpreter on multiple operating system threads (i.e. one interpreter on one operating system thread within the same process) which are invoked from the C/C++ application? Such applications may also have problems related to memory fragmentation and <a href="http://docs.python.org/c-api/init.html" rel="noreferrer">limitations of Py_Finalize()</a>.</p>
  
  <p>One such approach can be the following:</p>
  
  <ol>
  <li>Python thread and hence GIL disabled in pyconfig.h to keep it simple (#undef WITH_THREAD)</li>
  <li>All mutable global variables of Python Interpreter source code moved to heap-allocated struct referenced via Thread Local Storage (Reference: <a href="http://research.nokia.com/files/PythonOnAPhone.ppt" rel="noreferrer">Python on a Phone</a>).</li>
  </ol>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Is there any better approach?</li>
  <li>Are there any tools which can automate conversion of global variables of Python Interpreter source code to heap-allocated struct referenced via TLS (Thread Local Storage)?</li>
  </ol>
  
  <p>Similar topics are discussed here:</p>
  
  <ul>
  <li><a href="http://bytes.com/topic/python/answers/793370-multiple-independent-python-interpreters-c-c-program" rel="noreferrer">Multiple independent Python interpreters in a C/C++ program?</a></li>
  <li><a href="http://bytes.com/topic/python/answers/660081-multiple-python-interpreters-within-same-process" rel="noreferrer">Multiple python interpreters within the same process</a></li>
  <li><a href="http://lua-users.org/wiki/LuaVersusPython" rel="noreferrer">Lua Versus Python</a></li>
  </ul>