using python interpreters per thread in C++ program

Graham Dumpleton graham.dumpleton at gmail.com
Mon Sep 7 03:41:54 EDT 2009


On Sep 7, 3:42 pm, sturlamolden <sturlamol... at yahoo.no> wrote:
> On 7 Sep, 07:17, grbgooglefan <ganeshbo... at gmail.com> wrote:
>
> > What is best way to embed python in multi-threaded C++ application?
>
> Did you remeber to acquire the GIL? The GIL is global to the process
> (hence the name).
>
> void foobar(void)
> {
>     PyGILState_STATE state = PyGILState_Ensure();
>
>     /* Safe to use Python C API here */
>
>     PyGILState_Release(state);

You can't use that in this case as they specifically are talking about
a interpreter per thread, which implies creating additional sub
interpreters. The simplified GIL state API you mentioned only works
for threads operating in the main (first) interpreter created within
the process.

The OP can do what they want, but they need to user lower level
routines for creating their own thread state objects and acquiring the
GIL against them.

Graham



More information about the Python-list mailing list