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. QWebSocket and Synchronous Response/Request Cycle
QtWS25 Last Chance

QWebSocket and Synchronous Response/Request Cycle

Scheduled Pinned Locked Moved Unsolved General and Desktop
qwebsocketasynchronoussynchronousthreading
7 Posts 2 Posters 2.5k Views
  • 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.
  • A Offline
    A Offline
    ad7000
    wrote on 5 Apr 2018, 03:41 last edited by ad7000 4 May 2018, 03:46
    #1

    Hello,

    I am writing a wrapper around a QWebSocket interface, so that I can provide the application with a simple to use API for making REST like calls and receiving request synchronously. I am using a callback system in which stored callbacks get executed and removed from a queue once a signal is emitted, but I am having trouble implementing the public methods so that they block until the signal is emitted without also blocking the main UI thread.

    One such method is as follows:

    void ApHorizon::fetchFieldOptions(QString tName, Utility::ApCallback callback)
    {    
        ....
        // Some initialization
        ....
    
        m_messageQueue.append(payload);
        addCallback(tName, callback);
    
        emit messageQueueChanged(m_messageQueue);
    
        QEventLoop loop;
        auto handle = connect(this, &ApHorizon::serverMessageReceived, [tName, &loop](const QString type) {
            if (type == tName) loop.quit();
        });
        loop.exec();
        disconnect(handle);
    }
    

    That is, I want the method to block until a signal with a specific QString is received, and then continue after the fact, but at the same time without blocking the UI or other application functionality. I have tried moving the class instance to a separate thread but it seems the QEventLoop is working on the main thread no matter what thread I am in.

    A humble thanks if you can provide some insight.

    J 1 Reply Last reply 5 Apr 2018, 04:21
    0
    • A ad7000
      5 Apr 2018, 03:41

      Hello,

      I am writing a wrapper around a QWebSocket interface, so that I can provide the application with a simple to use API for making REST like calls and receiving request synchronously. I am using a callback system in which stored callbacks get executed and removed from a queue once a signal is emitted, but I am having trouble implementing the public methods so that they block until the signal is emitted without also blocking the main UI thread.

      One such method is as follows:

      void ApHorizon::fetchFieldOptions(QString tName, Utility::ApCallback callback)
      {    
          ....
          // Some initialization
          ....
      
          m_messageQueue.append(payload);
          addCallback(tName, callback);
      
          emit messageQueueChanged(m_messageQueue);
      
          QEventLoop loop;
          auto handle = connect(this, &ApHorizon::serverMessageReceived, [tName, &loop](const QString type) {
              if (type == tName) loop.quit();
          });
          loop.exec();
          disconnect(handle);
      }
      

      That is, I want the method to block until a signal with a specific QString is received, and then continue after the fact, but at the same time without blocking the UI or other application functionality. I have tried moving the class instance to a separate thread but it seems the QEventLoop is working on the main thread no matter what thread I am in.

      A humble thanks if you can provide some insight.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Apr 2018, 04:21 last edited by
      #2

      @ad7000 A synchronous API means a blocking API. If you call such an API from your main thread then it will block. If you don't want this then don't use synchronous APIs.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        ad7000
        wrote on 5 Apr 2018, 14:14 last edited by ad7000 4 May 2018, 14:14
        #3

        @jsulm Understood. I do want the API to be synchronous, but only within it's own thread, so that it can run in parallel with the main thread. This is what I am trying to figure out.

        J 1 Reply Last reply 5 Apr 2018, 14:53
        0
        • A ad7000
          5 Apr 2018, 14:14

          @jsulm Understood. I do want the API to be synchronous, but only within it's own thread, so that it can run in parallel with the main thread. This is what I am trying to figure out.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 5 Apr 2018, 14:53 last edited by
          #4

          @ad7000 Then use this in its own thread not in main thread.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply 5 Apr 2018, 15:50
          1
          • J jsulm
            5 Apr 2018, 14:53

            @ad7000 Then use this in its own thread not in main thread.

            A Offline
            A Offline
            ad7000
            wrote on 5 Apr 2018, 15:50 last edited by
            #5

            @jsulm I have tried having a QThread member variable in the class and moving the class instance to this thread in the class constructor, something like

            MyClass::MyClass(QObject *parent) : QObject(parent) {
                moveToThread(&m_myClassThread);
                m_myClassThread.start();
            }
            

            I have done this for several of the classes that use the API class, as well as on the API class itself, but the UI still blocks. Any suggestions?

            J 1 Reply Last reply 6 Apr 2018, 04:24
            0
            • A ad7000
              5 Apr 2018, 15:50

              @jsulm I have tried having a QThread member variable in the class and moving the class instance to this thread in the class constructor, something like

              MyClass::MyClass(QObject *parent) : QObject(parent) {
                  moveToThread(&m_myClassThread);
                  m_myClassThread.start();
              }
              

              I have done this for several of the classes that use the API class, as well as on the API class itself, but the UI still blocks. Any suggestions?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Apr 2018, 04:24 last edited by
              #6

              @ad7000 My suggestion would be to post more code, else it is hard to say why your UI is blocking...

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              A 1 Reply Last reply 7 Apr 2018, 14:35
              1
              • J jsulm
                6 Apr 2018, 04:24

                @ad7000 My suggestion would be to post more code, else it is hard to say why your UI is blocking...

                A Offline
                A Offline
                ad7000
                wrote on 7 Apr 2018, 14:35 last edited by
                #7

                @jsulm It's alright. I ended up going completely asynchronous by using a chained callback structure. Thanks anyways for the help!

                1 Reply Last reply
                1

                7/7

                7 Apr 2018, 14:35

                • Login

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