Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread(C++) and QTimer failed to call Python
Forum Updated to NodeBB v4.3 + New Features

QThread(C++) and QTimer failed to call Python

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 366 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    whuw
    wrote on last edited by whuw
    #1
    • goal: QThread(C++) child threads call deep learning Python training code, and display the training result (PyObject) on the Qt GUI in the main thread.

    • problem: the main thread displays training results in real time, using QTimer, which convert PyObject objects to C++ variables every 1 second. QTimer periodically accesses variables in PyObject, which will cause access conflicts when child threads call Python modules. The main thread calls the following code in real time:

      //Main thread real-time display function
      
      void DeepLearning::trainVis()
      {
          class PyThreadStateLock PyThreadLock; //Get Python GIL
      
          dictGuinfoRealtime = trainThread.dictGuinfoRealtime; //Passes the real-time results of the child thread to the main thread variable
      
          //elapsed_runtime
      
          //An attempt to access variables in dictGuinfoRealtime will result in an access conflict when the child thread calls the Python module
          PyObject* elapsedruntime_ = PyDict_GetItemString(dictGuinfoRealtime, "elapsed_runtime");
          Py_XDECREF(elapsedruntime_);
      
      }
      

    The child thread calls the Python training module as follows:

    ```C++
    //The child thread calls Python training code
    
    class PyThreadStateLock PyThreadLock; //Get Python GIL
    
    PyObject* pTrainS = PyImport_ImportModule("train");//"Train" is the file name of the training file 'train.py'
    
    PyObject* args = PyTuple_New(1);
    PyObject* TrainFun = PyObject_GetAttrString(pTrainSeg, "main");
    if (TrainFun && PyCallable_Check(TrainFun))
    {
    	PyTuple_SetItem(args, 0, dictGuinfoRealtime);
    
    	if (!PyObject_CallObject(TrainFun, args)) //Access conflicts are reported here during training
    	{
    		throw std::exception("PyObject_CallObject failed!");
    	}	
    }
    Py_XDECREF(TrainFun);
    ```
    
    • Struggled for a long time still did not find a solution, please give some advice, thank you!
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      Since your main code is in Python, why not use PySide6 for your GUI ? So you can stay with the same language.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      W 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Since your main code is in Python, why not use PySide6 for your GUI ? So you can stay with the same language.

        W Offline
        W Offline
        whuw
        wrote on last edited by
        #3

        @SGaist Thanks for your advice! This is because of requirements, the GUI interface must use C++

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ok, one thing that is not clear with your code explanation: why does the gui poll the algorithm execution every second for results ? Shouldn't the object handling that execution notify the other elements of the application once they are done ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved