FAQ
I have 2 threads in C code using python 2.5.2. First thread creates
new interpreter (i need several interpreters but those 2 threads use
only one) like that:

PyEval_AcquireLock();
threadState = Py_NewInterpreter();
PyThreadState_Swap(threadState);

// calling python API

PyThreadState_Swap(NULL);
PyEval_ReleaseLock();

Second thread uses interpreter created in first thread:

PyEval_AcquireLock();
PyThreadState_Swap(threadState);

and sometimes PyThreadState_Swap crashes in debug build
(PyGILState_GetThisThreadState() returns garbage). In release build
that code doesn't run and so far no other problem was found.
I call PyEval_InitThreads() at the begining of program and every
PyEval_AcquireLock() has PyEval_ReleaseLock().

Am I allowed to use the same threadState in different threads?
If I am, is there another problem in my code?
Or maybe it's a bug in python - acording to documentation "Python
still supports the creation of additional interpreters (using
Py_NewInterpreter()), but mixing multiple interpreters and the
PyGILState_*() API is unsupported." - I don't use PyGILState_ but it's
used internally in PyThreadState_Swap(). I also don't use
PyEval_RestoreThread() - comment sugests that crashing code is present
because possibility of calling from PyEval_RestoreThread().

Search Discussions

  • Philip Semanchuk at Apr 5, 2011 at 1:15 am

    On Apr 4, 2011, at 9:08 AM, Wiktor Adamski wrote:

    I have 2 threads in C code using python 2.5.2. First thread creates
    new interpreter (i need several interpreters but those 2 threads use
    only one) like that:

    PyEval_AcquireLock();
    threadState = Py_NewInterpreter();
    PyThreadState_Swap(threadState);

    // calling python API

    PyThreadState_Swap(NULL);
    PyEval_ReleaseLock();

    Second thread uses interpreter created in first thread:

    PyEval_AcquireLock();
    PyThreadState_Swap(threadState);

    and sometimes PyThreadState_Swap crashes in debug build
    (PyGILState_GetThisThreadState() returns garbage). In release build
    that code doesn't run and so far no other problem was found.
    I call PyEval_InitThreads() at the begining of program and every
    PyEval_AcquireLock() has PyEval_ReleaseLock().

    Am I allowed to use the same threadState in different threads?
    If I am, is there another problem in my code?
    Or maybe it's a bug in python - acording to documentation "Python
    still supports the creation of additional interpreters (using
    Py_NewInterpreter()), but mixing multiple interpreters and the
    PyGILState_*() API is unsupported." - I don't use PyGILState_ but it's
    used internally in PyThreadState_Swap(). I also don't use
    PyEval_RestoreThread() - comment sugests that crashing code is present
    because possibility of calling from PyEval_RestoreThread().
    Hi Wiktor,
    I'm sorry I don't have a solution or even a suggestion for you. I just wanted to point out that PyEval_AcquireLock() and PyEval_ReleaseLock() were recently deprecated:
    http://bugs.python.org/issue10913

    Obviously they'll be around for quite a while longer but given the ominous-but-vague warning in issue10913's description, you might want to stay away from them. It's frustrating for me because I've got code I can't get to work without them.

    Good luck
    Philip

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 4, '11 at 1:08p
activeApr 5, '11 at 1:15a
posts2
users2
websitepython.org

People

Translate

site design / logo © 2022 Grokbase