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. Qt 6, Qt.Future .then freezess app

Qt 6, Qt.Future .then freezess app

Scheduled Pinned Locked Moved Unsolved General and Desktop
qfuturethread
6 Posts 3 Posters 446 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by Dariusz
    #1

    Hey

    More or less my code >

    auto reply = mNetworkAccessManager->get(initialRequest);//, j.dump().c_str());
        
            auto o = QtFuture::connect(reply, &QNetworkReply::finished)
                    .then(this, [this, reply] {
                        const auto data = icuj::getJsonFromRawString(reply->readAll());
                        qDebug() << data;
                        qDebug() << reply;
                        return reply;
                    });
    

    Intended to be returned from function as "backend-pre-process-logic" so that I can attach logic after it.

            o.then(this, [](QNetworkReply *reply) {
                qDebug() << "secondary callback?";
                return reply;
            });
    

    However when I try to daisy chain second .then, from o, then the app freezes.
    Any ideas? I'm a little lost.

    I know I can do
    .then()
    .then()
    .then()
    But I want to do thens, in different functions/handlers...

    Edit, ok I take it back, I cant do .then().then().then(), second then freezes app o.o

    Pl45m4P 1 Reply Last reply
    0
    • D Offline
      D Offline
      Dariusz
      wrote on last edited by
      #2

      Ok seems like the issue was thread... passing context > this, caused it to somehow self lock o.O

      1 Reply Last reply
      0
      • D Dariusz

        Hey

        More or less my code >

        auto reply = mNetworkAccessManager->get(initialRequest);//, j.dump().c_str());
            
                auto o = QtFuture::connect(reply, &QNetworkReply::finished)
                        .then(this, [this, reply] {
                            const auto data = icuj::getJsonFromRawString(reply->readAll());
                            qDebug() << data;
                            qDebug() << reply;
                            return reply;
                        });
        

        Intended to be returned from function as "backend-pre-process-logic" so that I can attach logic after it.

                o.then(this, [](QNetworkReply *reply) {
                    qDebug() << "secondary callback?";
                    return reply;
                });
        

        However when I try to daisy chain second .then, from o, then the app freezes.
        Any ideas? I'm a little lost.

        I know I can do
        .then()
        .then()
        .then()
        But I want to do thens, in different functions/handlers...

        Edit, ok I take it back, I cant do .then().then().then(), second then freezes app o.o

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Dariusz said in Qt 6, Qt.Future .then freezess app:

            o.then(this, [](QNetworkReply *reply) {
                qDebug() << "secondary callback?";
                return reply;
            });
        

        What if you chain them directly?
        Maybe o.then() is the problem?!

        auto reply = mNetworkAccessManager->get(initialRequest);//, j.dump().c_str());
            
                auto o = QtFuture::connect(reply, &QNetworkReply::finished)
                        .then(this, [this, reply] {
                            const auto data = icuj::getJsonFromRawString(reply->readAll());
                            qDebug() << data;
                            qDebug() << reply;
                            return reply;
                      }).then(this, [](QNetworkReply *reply) {           
                            qDebug() << "secondary callback?";           
                            return reply;           
                      });
        

        @Dariusz said in Qt 6, Qt.Future .then freezess app:

        .then()
        .then()
        .then()

        and then? aaaand then? :D


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        D 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @Dariusz said in Qt 6, Qt.Future .then freezess app:

              o.then(this, [](QNetworkReply *reply) {
                  qDebug() << "secondary callback?";
                  return reply;
              });
          

          What if you chain them directly?
          Maybe o.then() is the problem?!

          auto reply = mNetworkAccessManager->get(initialRequest);//, j.dump().c_str());
              
                  auto o = QtFuture::connect(reply, &QNetworkReply::finished)
                          .then(this, [this, reply] {
                              const auto data = icuj::getJsonFromRawString(reply->readAll());
                              qDebug() << data;
                              qDebug() << reply;
                              return reply;
                        }).then(this, [](QNetworkReply *reply) {           
                              qDebug() << "secondary callback?";           
                              return reply;           
                        });
          

          @Dariusz said in Qt 6, Qt.Future .then freezess app:

          .then()
          .then()
          .then()

          and then? aaaand then? :D

          D Offline
          D Offline
          Dariusz
          wrote on last edited by Dariusz
          #4

          @Pl45m4 For some reason passing this as context caused mutex lock!
          I'm now trying to figure out how to perform multiple/nested "then's"....

          Like >

          QFuture<int> networkInit() {
              QNetworkRequest initialRequest(QUrl("some fancy url"));
              auto reply = mNetworkAccessManager->get(initialRequest);//, j.dump().c_str());
              auto o = QtFuture::connect(reply, &QNetworkReply::finished)
                      .then([reply] {
                          auto data = reply->readAll()
                          return data;
                      }).then([](QByteArray reply) {
                  return 1;
              })
              QObject::connect(reply, &QNetworkReply::finished, reply, QNetworkReply::deleteLater, Qt::QueuedConnection);
              return o
          }
          ///This func breaks :- ) 
          QFuture<QNetworkReply *> requestData(QString adr) {
              return networkInit().then([this, adr](int status) {
                  if (status == 0) {
                      QNetworkRequest initialRequest(QUrl(adr));
                      auto reply = mNetworkAccessManager->get(initialRequest);
                      auto futReply= QtFuture::connect(reply, &QNetworkReply::finished)
                              .then([reply] {
                                  return reply;
                              });
                      return futReply;
                  }
                  // raise Exception ???
              });
          }
          

          /// But if the one above works then I get >

          Adding a continuation to a future which already has a continuation. The existing continuation is overwritten.		
          (C:\Users\qt\work\qt\qtbase\src\corelib\thread\qfutureinterface.cpp:940, void __cdecl QFutureInterfaceBase::setContinuation(class std::function<void __cdecl(class QFutureInterfaceBase const &)>,class QFutureInterfaceBasePrivate *))
          
          int main() {
              auto future = requestData("someFancyDat")
              future.then([](QNetworkReply *reply) {
                  qDebug() << reply->readAll();
              });
          }
          

          @Pl45m4 said in Qt 6, Qt.Future .then freezess app:

          and then? aaaand then? :D

          lolz that's hilarious XDDD

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

            Hi,

            One issue with your main function is that it does not create any QCoreApplication object hence Qt's internal state is not properly setup.

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

            D 1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              One issue with your main function is that it does not create any QCoreApplication object hence Qt's internal state is not properly setup.

              D Offline
              D Offline
              Dariusz
              wrote on last edited by Dariusz
              #6

              @SGaist I have QApplication - gui one. ah yes the int main() is just a quick hack/example... there full widget app with buttons running :- )

              Also I got it to work, I ended up getting nested futures, and the path to fix that is .unwrap()>https://doc.qt.io/qt-6/qfuture.html#unwrap
              Seems to work! :D

              Threading and linking to context still seem to be broken tho o.o

              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