[PyKDE] embedding PyQt in a c++ app

Nicolas Decoster my_name at my_company.fr
Sat Jul 31 12:15:01 BST 2004


Hi.

On Thu, Jul 29, 2004 at 05:21:25PM +0200, Christoph Wiedemann wrote:
> Hello,
> 
> i have problems to embed PyQt in a c++ application. Since the application
> uses threads and multiple python interpreters, i must ensure that the
> correct thread state is set and the GIL is locked, whenever the python API
> is used. I do that with the
> 
>   PyEval_RestoreThread(threadState);
> 
> and
> 
>   PyEval_SaveThread();
> 
> methods. This works fine, as long as i don't use widgets created with PyQt,
> because there are many "callbacks" (virtual methods), e.g. mousePressEvent,
> which are called directly from PyQt. 
> 

I had the same problem. But since this wasn't a major issue for my
project, I gave up without investigating. But I am interested in the
solution.

> Is there any way to force PyQt to acquire the GIL before those virtual
> methods are called?
> 
> Hmm, this wouldn't solve the issue with multiple interpreters; each
> interpreter has its own thread state, which must be restored before using
> the API. 

I do it something like that:

  PyThreadState* _pyThreadState;
  _pyThreadState = Py_NewInterpreter();

then later

  PyEval_AcquireLock();
  PyThreadState_Swap(_pyThreadState);

  ... some python stuff ...

  PyThreadState_Swap(NULL);
  PyEval_ReleaseLock();

and when I'm done

  PyEval_AcquireLock();
  PyThreadState_Swap(_pyThreadState);

  Py_EndInterpreter(_pyThreadState);

  PyThreadState_Swap(NULL);
  PyEval_ReleaseLock();



Nicolas.

--
Nicolas Decoster
Noveltis - Parc Technologique du Canal
2, avenue de l'Europe - 31526 Ramonville Saint Agne Cedex - France
Tél. : 00 (33) 5 62 88 11 16  -  Fax  : 00 (33) 5 62 88 11 12




More information about the PyQt mailing list