13

This is a follow up to Call Python from C++

At the startup of the programm I call the following function to initialize the interpreter:

void initPython(){
    PyEval_InitThreads();
    Py_Initialize();
    PyEval_ReleaseLock();
}

Every thread creates it's own data structure and acquires the lock with:

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
//call python API, process results
PyGILState_Release(gstate);

Rather straight forward once you understood the GIL, but the problem is that I get a segfault when calling Py_Finalize().

void exitPython(){
    PyEval_AcquireLock();
    Py_Finalize();
}

The reference is rather dubious about Py_Finalize() (or maybe I'm just reading it the wrong way) and I'm not sure if PyEval_AcquireLock() can acquire the lock if there are some active threads and what happens if there are active threads when Py_Finalize() is called.

Anyways, I get a segfault even if I'm sure that all threads have finished their work, but only if at least one was created. E.g. calling initPython() followed from exitPython() creates no error.

I could just ignore the problem and hope the OS knows what it does, but I'd prefere if I could figure out what is going on..

1

3 Answers 3

7

Yeah the whole section is rather dubious but I think I've got my mistake.

I've got to save the PyThreadState when initializing the interpreter and swap this state back in when I finish it (no idea why I need a specific ThreadState to call Finalize - shouldn't every State work as well?)

Anyways the example if other people got the same problem:

PyThreadState *mainstate;

void initPython(){
    PyEval_InitThreads();
    Py_Initialize();
    mainstate = PyThreadState_Swap(NULL);
    PyEval_ReleaseLock();
}

void exitPython(){
    PyEval_AcquireLock();
    PyThreadState_Swap(mainstate);
    Py_Finalize();
}

The only problem with this is, that I can acquire the lock like every other thread, even if there are still threads working. The API doesn't mention what happens when Finalize() is called while other threads are still working. Sounds like the perfect example of a race condition..

1

Have you tried commenting out all the 'work' done in your threads? Replace it with a busy loop or a sleep or something. That will help pinpoint whether it is your initialisation/shutdown code, or something you're actually doing to Python in between. Perhaps you're not setting up the threads properly - there are a lot of thread-specific functions in the C API and I'm not sure which ones you need to ensure proper operation.

3
  • I've commented everything out but PyGILState_Ensure() and Release() and the error still occurs. If I comment them out as well there's no problem..
    – Voo
    Sep 16, 2009 at 11:01
  • In that case, I'm guessing there's something about the thread management that is not being done properly. Unfortunately the relevant C API page with all the thread functions on it is far from obvious as to which of those calls you need.
    – Kylotan
    Sep 16, 2009 at 14:55
  • The "calls you need" most of the time are Py_BEGIN_ALLOW_THREADS and its counterpart. Those are, in turn, macros that use PyEval_SaveThread() and its counterpart. So if I were writing something like OP, I'd follow that example.
    – Kevin
    Jan 22, 2015 at 5:03
1

I am also getting similar problem while running scripts containing pyxhook from different threads through embedded interpreter.

There is no problem if one script running at a time. The hook is released properly but if two or more script ran in parallel the hooking is not stopped. Though my scripts returned properly and cancel() from pyxhook also returned properly, I think still some threads are running related to xlib. This pyxhook problem I have solved by keeping a global flag to watch if pyxhook is already running and not re-initializing pyxhook from every thread.

Now regarding Py_Finalize(), if pyxhook is re-initialized in every thread:

if I do not call PyEval_AcquireLock() and PyThreadState_Swap() before calling Py_Finalize() it terminates in Linux but not in Win32. In Win32 there is a problem if I do not go through PyEval_AcquireLock() and PyThreadState_Swap().

For the time being the temporary solution for me is to terminate differently in two different OS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.