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. How to Implement an Async Server with QDBus?
QtWS25 Last Chance

How to Implement an Async Server with QDBus?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 229 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.
  • L Offline
    L Offline
    lkimuka
    wrote on last edited by
    #1

    My current QDBus server interface is synchronous. If the interface takes a long time to process, it causes blocking. How can I modify the server-side implementation to make the calls asynchronous?

    Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS SGaist

      I don't see any server in there. You are using the system's server.

      As for your question, I think what you are looking for is https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies

      L Offline
      L Offline
      lkimuka
      wrote on last edited by
      #5

      @SGaist Thank you very much. I read https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies before asking this question, but that wasn't what I was looking for. The D-Bus adaptors in my project are automatically generated with CMake, and the behavior isn't correct when I directly use the extra QDBusMessage parameter at the end of the search() parameters.

      And I figured out how to do it after a few tries. The nitty-gritty of solving the problem is using Q_EMIT to send an async completion signal to the client, with the results passed along at that point.

      Here is a simple example to demonstrate the solution:

      public slots:
          // Asynchronously search
          Q_NOREPLY void async_search(QString keywords) {
              ...
      
              // Pass the results and notify the client once the time-consuming logic is finished
              Q_EMIT asyncSearchCompleted("test result");
          }
      
      Q_SIGNALS:
          void asyncSearchCompleted(const QString& result);
      

      To handle the results on the client side, we connect the signal to a slot:

      QDBusInterface *iface = ...;
      connect(iface, SIGNAL(asyncSearchCompleted(QString)), this, SLOT(handleSearchResult(QString)));
      
      public slots:
          void handleSearchResult(const QString &result) {
              qDebug() << "Received async search result:" << result;
          }
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        How did you implement your server ?

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

        L 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          How did you implement your server ?

          L Offline
          L Offline
          lkimuka
          wrote on last edited by
          #3

          @SGaist Hello,

          Here are the header and implementation files for my QDBus server:
          https://github.com/dxnu/deepin-anything/blob/master/src/server/include/core/base_event_handler.h

          https://github.com/dxnu/deepin-anything/blob/master/src/server/src/core/base_event_handler.cpp

          The base_event_handler class is my QDBus server implementation. I want to make the search() interface asynchronous.

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

            I don't see any server in there. You are using the system's server.

            As for your question, I think what you are looking for is https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies

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

            L 1 Reply Last reply
            0
            • SGaistS SGaist

              I don't see any server in there. You are using the system's server.

              As for your question, I think what you are looking for is https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies

              L Offline
              L Offline
              lkimuka
              wrote on last edited by
              #5

              @SGaist Thank you very much. I read https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies before asking this question, but that wasn't what I was looking for. The D-Bus adaptors in my project are automatically generated with CMake, and the behavior isn't correct when I directly use the extra QDBusMessage parameter at the end of the search() parameters.

              And I figured out how to do it after a few tries. The nitty-gritty of solving the problem is using Q_EMIT to send an async completion signal to the client, with the results passed along at that point.

              Here is a simple example to demonstrate the solution:

              public slots:
                  // Asynchronously search
                  Q_NOREPLY void async_search(QString keywords) {
                      ...
              
                      // Pass the results and notify the client once the time-consuming logic is finished
                      Q_EMIT asyncSearchCompleted("test result");
                  }
              
              Q_SIGNALS:
                  void asyncSearchCompleted(const QString& result);
              

              To handle the results on the client side, we connect the signal to a slot:

              QDBusInterface *iface = ...;
              connect(iface, SIGNAL(asyncSearchCompleted(QString)), this, SLOT(handleSearchResult(QString)));
              
              public slots:
                  void handleSearchResult(const QString &result) {
                      qDebug() << "Received async search result:" << result;
                  }
              
              1 Reply Last reply
              1
              • SGaistS SGaist has marked this topic as solved on

              • Login

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