Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83320

Re: PyGILState API and Py_Main

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'handler': 0.05; 'error:': 0.07; 'matched': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thus,': 0.09; 'api': 0.11; 'python': 0.11; 'thread': 0.14; 'cleaned': 0.16; 'creation.': 0.16; 'expects': 0.16; 'other,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:API': 0.16; ':-)': 0.16; 'acquired': 0.19; 'advance.': 0.19; 'skip:p 40': 0.19; 'header:User-Agent:1': 0.23; 'error': 0.23; 'char': 0.24; 'script': 0.25; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'code': 0.31; 'piece': 0.31; 'void': 0.31; 'writes:': 0.31; 'interface': 0.32; 'minimal': 0.33; 'skip:_ 10': 0.34; 'but': 0.35; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'should': 0.36; 'level': 0.37; 'to:addr:python- list': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'release': 0.40; 'skip:u 10': 0.60; 'removing': 0.60; 'simple': 0.61; 'back': 0.62; 'received:217': 0.63; 'high': 0.63; 'deals': 0.65; 'feeling': 0.68; 'default': 0.69; 'safe': 0.72; 'ret;': 0.84; 'notion': 0.91; 'state.': 0.95; 'yourself,': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From dieter <dieter@handshake.de>
Subject Re: PyGILState API and Py_Main
Date Thu, 08 Jan 2015 08:22:32 +0100
References <54928B82.1080309@cea.fr>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Gmane-NNTP-Posting-Host pd9e0a34b.dip0.t-ipconnect.de
User-Agent Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux)
Cancel-Lock sha1:uiEpUe6wO4p7Arn194vmY6Ra06M=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17466.1420701874.18130.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420701874 news.xs4all.nl 2944 [2001:888:2000:d::a6]:39735
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83320

Show key headers only | View raw


Adrien Bruneton <adrien.bruneton@cea.fr> writes:

> I am having a hard time understanding what is the proper use of
> PyGILState_Ensure/Release.
> My understanding is that one should always be matched with the other,
> and that this high level API auto-magically deals with the ThreadState
> creation.
>
> However the following piece of code (executed with a simple "print
> 'hello world' " script as argv) triggers the message:
>
>     Fatal Python error: auto-releasing thread-state, but no
> thread-state for this thread

Each function at the Python C interface has (with high probability)
a notion whether it is called from Python (GIL acquired) or from pure C code
(GIL not acquired). When you call the function yourself, you must match
this expectation. "Py_Main" likely expects to be called without
acquired GIL. Thus, likely, you should not call "PyGILState_Ensure"
before "Py_Main".

> Minimal code:
>
> void initPython(int initsigs)
> {
>   if (Py_IsInitialized() == 0)
>     {
>       Py_InitializeEx(initsigs);
>       // Put default SIGINT handler back after
> Py_Initialize/Py_InitializeEx.
>       signal(SIGINT, SIG_DFL);
>     }
>
>   int threadInit = PyEval_ThreadsInitialized();
>   PyEval_InitThreads(); // safe to call this multiple time
>
>   if(!threadInit)
>     PyEval_SaveThread(); // release GIL
> }
>
> int main(int argc, char ** argv)
> {
>   initPython(1);
>   PyGILState_STATE _gstate_avoid_clash = PyGILState_Ensure();
>   int ret = Py_Main(argc, argv);
>   PyGILState_Release(_gstate_avoid_clash);  // this one triggers the
> Fatal error
>   Py_Finalize();
>   return ret;
> }
>
>
> Removing the last PyGILState_Release works, but I have a bad feeling
> about it :-)
> Any help would be welcome! Thanks in advance.

Likely, "Py_Main" has already released the GIL and cleaned up
the thread state.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: PyGILState API and Py_Main dieter <dieter@handshake.de> - 2015-01-08 08:22 +0100

csiph-web