Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen is PyEval_InitThreads meant to be called?
    primarykey
    data
    text
    <p>I'm a bit confused about when I'm supposed to call <code>PyEval_InitThreads</code>. In general, I understand that <code>PyEval_InitThreads</code> must be called whenever a non-Python thread (i.e. a thread that is spawned within an extension module) is used. </p> <p>However, I'm confused if <code>PyEval_InitThreads</code> is for C programs which embed the Python interpreter, or Python programs which import C-extension modules, or both.</p> <p>So, if I write a C extension module that will internally launch a thread, do I need to call <code>PyEval_InitThreads</code> when initializing the module?</p> <p>Also, <code>PyEval_InitThreads</code> <a href="http://docs.python.org/dev/c-api/init.html#PyEval_InitThreads"><strong>implicitly acquires the Global Interpreter Lock</strong></a>. So after calling <code>PyEval_InitThreads</code>, presumably the GIL must be <em>released</em> or deadlock will ensue. So how do you release the lock? After reading the documentation, <a href="http://docs.python.org/dev/c-api/init.html#PyEval_ReleaseLock"><strong><code>PyEval_ReleaseLock()</code></strong></a> appears to be the way to release the GIL. However, in practice, if I use the following code in a C extension module:</p> <pre><code> PyEval_InitThreads(); PyEval_ReleaseLock(); </code></pre> <p>...then at runtime Python aborts with:</p> <pre><code>Fatal Python error: drop_gil: GIL is not locked </code></pre> <p>So how do you release the GIL after acquiring it with <code>PyEval_InitThreads</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload