Support for Python threads

2012-11-02
2015-05-12
  • Chris Meyer
    Chris Meyer
    2012-11-02

    I'd like to extend PythonQt to support Python threads. This mainly consists of adding guard code (macros) to swap in the thread state before calls into the Python library.

    Has anyone had experience with this issue?

    If I make the changes, would there be interest in integrating them into the repository?

     
  • Florian Link
    Florian Link
    2012-11-02

    I considered the effort and one part are the GIL macros that you will need.
    The other part is that you have to ensure thread-safety in the global PythonQt structures (the class info hashes etc.). I suggest to make it an optional feature (so that it can be compiled with and without thread support, e.g. using a #ifdef PYTHONQT_WITH_THREADSUPPORT macro).

    You need to find all places where the code enters Python and also all places where Qt is entered (to release the GIL while being in Qt).

    I am willing to integrate you changes if they can be turned off at compile time.

     
  • Chris Meyer
    Chris Meyer
    2012-11-02

    Great. I've started the work and it looks like there can be a simple macro that gets inserted into the beginning of each function that calls into Python. When threads are enabled it will define an auto class that handles the GIL. Something like this:

    class PythonQtThreadStateSaver
    {
    public:
      PythonQtThreadStateSaver() { _gstate = PyGILState_Ensure(); }
      ~PythonQtThreadStateSaver() { PyGILState_Release(_gstate); }
    private:
      PyGILState_STATE _gstate;
    };
    #ifdef PYTHONQT_WITH_THREADSUPPORT
    #define PYTHONQT_THREAD_SAVER PythonQtThreadStateSaver threadSaver;
    #else
    #define PYTHONQT_THREAD_SAVER
    #endif
    

    I'll look into the Qt side of things next.

     
  • Jeff Ward
    Jeff Ward
    2015-05-12

    I’m trying to run multiple python threads simultaneously but am encountering thread blocking that I don’t know the best way of solving. The threads run fine through Python alone but not when run from a Qt UI with PythonQt. The last post for this topic was in 2012. Has support for Python threads been added since then or is there a workaround?
    Other related topics within this forum that I found are:
    PythonQt and Garbage Collection (Last Post 2011)
    Threadsafe PythonQt (Last Post 2010)

     
  • Florian Link
    Florian Link
    2015-05-12

    As far as I know, this is not yet supported.
    It would require to

    a) make PythonQt thread-safe
    b) release/lock the GIL whenever entering Qt/Python