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. Use QNetworkAccessManager from multiple threads
Forum Updated to NodeBB v4.3 + New Features

Use QNetworkAccessManager from multiple threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 10.1k Views 3 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.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #2

    @RudolfVonKrugstein
    The problem is that QNetworkAccessManager::get is creating an object - QNetworkReply with a parent QNetworkAccessManager. This is not allowed because your networkManagerobject lives in another thread. Unfortunately, I don't know how to work around that problem in the high-level threading API, hopefully someone else will pitch in and suggest something.

    Kind regards.

    Read and abide by the Qt Code of Conduct

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

      Hi,

      Like @kshegunov wrote, you can't do that the way you are currently doing it. If you really want to move everything in another thread, you should use the worker object approach.

      However, first thing to analyze, why do you need that blocking behavior rather than the asynchronous ?

      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
      • R Offline
        R Offline
        RudolfVonKrugstein
        wrote on last edited by
        #4

        @SGaist: Well, the background is how I want to structure my code.

        I want to write:

        QtConcurrent::run([=](){
            prepareRequest();
            doRequest();
            workWithResult();
        });
        

        arguably, one could also write:

        prepareRequest();
        QNetworkResult* res = doRequest();
        connect(res,&QNetworkResult::finished,[=](){
            workWithResult();
        });
        

        But then I want to have a function, which does the network request somewhere deep inside it and return something based on the result of the request. I could pass a callback parameter to this function, that would work, but I find this tedious when the structures get deeper.

        Did my intention become clear? I am open to alternatives.

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

          There's something not quiet clear with your description.
          You want to:

          1. Use thread to make synchronous network request
          2. Have a function that calls the request method of that thread and return the result of the processing of the reply.

          Do I get you right ?

          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
          • R Offline
            R Offline
            RudolfVonKrugstein
            wrote on last edited by
            #6

            Yes, I that is correct

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

              Doesn't that defeat the purpose of the thread ?

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

              R 1 Reply Last reply
              0
              • SGaistS SGaist

                Doesn't that defeat the purpose of the thread ?

                R Offline
                R Offline
                RudolfVonKrugstein
                wrote on last edited by
                #8

                @SGaist what exactly to you mean? What does defeat the purpose of the thread?

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

                  Unless I misunderstood you, you are making an asynchronous function synchronous, move that logic to another thread so it doesn't block your GUI, then call a function on that thread that will block until it gets the answer. Is that correct ? If so, I don't see the advantage of the thread at all.

                  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
                  • R Offline
                    R Offline
                    RudolfVonKrugstein
                    wrote on last edited by
                    #10

                    Well the point is, that I want to have multiple network operations (not just one) chained together and with logic between them.

                    I believe that with blocking operations the code is much more readable. Here is a pseudo code example:

                    QString getRandomWebsite() {
                        int randomNumber = extractNumber(blockingRequest("http://random-number-site.org/2");
                        switch (randomNumber) {
                          case 1:
                            return blockingRequest("http://website1.org");
                      case 2:
                            return blockingRequest("http://website1.org");
                        }
                    }
                    
                    int countRandomWordInRandomWebsite() {
                        QString website = getRandomWebsite();    
                        QString randomWord = blockingRequest("http://random-number-site.org/random_word");
                        emit requestResult(website.count(randomWord));
                    }
                    

                    If I would implement this using asynchronous calls, I believe it looses it readability (but I might not have thought yet of the ideal solution ...).

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

                      QNetworkAccessManager handles up to 6 network operations in parallel.

                      You can use lambdas to make the code more compact and understandable.

                      Watch out, you don't return anything in countRandomWordInRandomWebsite. You should have a compiler warning about that.

                      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