title: Embedded Python: Multiple Sub-Interpreters not working
body: <p>I'm trying to understand sub-interpreters and GIL. But my experiment is failing often(The same code rarely works).</p>
  
  <p>Gathering info from SO questions and few sites, I have the following code which spawns 2 non-python threads. Each of these threads are given a python sub-interpreter. I want to release GIL inside these threads and call a DLL function in C++(though this example does not detail that. Here I just write to stdout). Basically I want to see concurrency in the execution(Non-Python DLL invocation).</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;Python.h&gt;
  
  void worker(PyInterpreterState* interp, int n) 
  {
      PyThreadState* ts;
      ts = PyThreadState_New(interp);
  
      PyThreadState_Swap(ts);
  
      PyThreadState* _save;
      _save = PyEval_SaveThread();
  
      std::cout &lt;&lt; n &lt;&lt; std::endl;  // Non-Python execution. My Focus.
  
      PyEval_RestoreThread(_save);
  
      PyThreadState_Swap(ts);
  
      PyThreadState_Clear(ts);
      PyThreadState_DeleteCurrent();
  
      return;
  }
  
  
  int main()
  {
      try 
      {
          Py_Initialize();
  
          PyEval_InitThreads();
  
          PyThreadState* _main = PyThreadState_Get();
  
          PyThreadState* i1 = Py_NewInterpreter();
          PyThreadState* i2 = Py_NewInterpreter();
  
          std::thread t1(worker, i1-&gt;interp, 1);
          std::thread t2(worker, i2-&gt;interp, 2);
  
          t1.join();
          t2.join();
  
          PyThreadState_Swap(i1);
          PyThreadState_Clear(i1);
          Py_EndInterpreter(i1);
          PyThreadState_Swap(i2);
          PyThreadState_Clear(i2);
          Py_EndInterpreter(i2);
  
          PyThreadState_Swap(_main);
  
          Py_Finalize();
          return 0;
      }
      catch(std::exception&amp; e)
      {
          std::cout &lt;&lt; "Exception:" &lt;&lt; std::endl &lt;&lt; e.what() &lt;&lt; std::endl;
      }
  }
  
  </code></pre>
  
  <p>Running a single thread works all the time. When I run 2 threads as shown, I get any of the following outputs.</p>
  
  <ol>
  <li>In <code>PyEval_SaveThread()</code>, </li>
  </ol>
  
  <pre class="lang-sh prettyprint-override"><code>2
  Fatal Python error: drop_gil: GIL is not locked
  Python runtime state: initialized
  
  Current thread 0x00002d08 (most recent call first):
  &lt;no Python frame&gt;
  </code></pre>
  
  <ol start="2">
  <li>In <code>PyEval_SaveThread()</code>,</li>
  </ol>
  
  <pre class="lang-sh prettyprint-override"><code>1
  Fatal Python error: PyEval_SaveThread: NULL tstate
  Python runtime state: initialized
  
  Current thread 0x00003eb8 (most recent call first):
  &lt;no Python frame&gt;
  </code></pre>
  
  <p>Either of the thread succeeds, the other one fails.</p>
  
  <ol start="3">
  <li>Rarely works. The same code.</li>
  </ol>
  
  <pre><code>1
  2
  </code></pre>
  
  <p>Can someone shed some light on this? Need help. Thanks.</p>