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

How to use QNetworkAccessManager in multiple threads

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 4.5k Views 2 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.
  • MozzieM Mozzie

    @CP71

    right, and there is another question:
    Will it be too resource intensive, or Will creating too many QNetworkAccessManager cause other problems?

    CP71C Offline
    CP71C Offline
    CP71
    wrote on last edited by
    #6

    @Mozzie
    Sorry but I don't know, I have never needed of multiple instance of QNetworkAccessManager.
    My answer is only linked to:
    "Parent is QNetworkAccessManager(0x1a26fe50b80), parent's thread is QThread(0x1a270705560), current thread is QThread(0x1a270704ae0"

    MozzieM 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Mozzie Is there really a need to use it in multiple threads? It has an asynchronous API.

      MozzieM Offline
      MozzieM Offline
      Mozzie
      wrote on last edited by
      #7

      @jsulm

      what is the difference between asynchronous API and multi thread?

      if I use asynchronous API and have a lot of request, when data is coming, will it block my gui thread?

      J.HilkJ jsulmJ 2 Replies Last reply
      0
      • CP71C CP71

        @Mozzie
        Sorry but I don't know, I have never needed of multiple instance of QNetworkAccessManager.
        My answer is only linked to:
        "Parent is QNetworkAccessManager(0x1a26fe50b80), parent's thread is QThread(0x1a270705560), current thread is QThread(0x1a270704ae0"

        MozzieM Offline
        MozzieM Offline
        Mozzie
        wrote on last edited by
        #8

        @CP71
        ok, thanks

        1 Reply Last reply
        1
        • MozzieM Mozzie

          @jsulm

          what is the difference between asynchronous API and multi thread?

          if I use asynchronous API and have a lot of request, when data is coming, will it block my gui thread?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by SGaist
          #9

          @Mozzie said in How to use QNetworkAccessManager in multiple threads:

          if I use asynchronous API and have a lot of request, when data is coming, will it block my gui thread?

          No it will not block, that's the point of asynchronous apis.

          Keep in mind, QNetWorkAccessManger is limited to 6 parallel get requests, if you send more, those will be queued and executed when one of the 6 running requests finishes. FIFO

          [edit: Fixed number of parallel requests SGaist]


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          MozzieM 1 Reply Last reply
          5
          • MozzieM Mozzie

            @jsulm

            what is the difference between asynchronous API and multi thread?

            if I use asynchronous API and have a lot of request, when data is coming, will it block my gui thread?

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #10

            @Mozzie
            The difference between asynchronous API and multithreading is that you do not have to handle multithreading which can be quite complex and error-prone.

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

            1 Reply Last reply
            3
            • J.HilkJ J.Hilk

              @Mozzie said in How to use QNetworkAccessManager in multiple threads:

              if I use asynchronous API and have a lot of request, when data is coming, will it block my gui thread?

              No it will not block, that's the point of asynchronous apis.

              Keep in mind, QNetWorkAccessManger is limited to 6 parallel get requests, if you send more, those will be queued and executed when one of the 6 running requests finishes. FIFO

              [edit: Fixed number of parallel requests SGaist]

              MozzieM Offline
              MozzieM Offline
              Mozzie
              wrote on last edited by
              #11

              @J-Hilk

              sorry, I did not make it clear.

              I mean, if use asyn apis, I need to use signal and slots, right?
              so if I use Qt::QueueConnection, my data will be processed in the gui thread, if the process need a lot of time, will it block my gui thread?
              or should I use Qt::DirectConnection?

              J.HilkJ 1 Reply Last reply
              0
              • MozzieM Mozzie

                @J-Hilk

                sorry, I did not make it clear.

                I mean, if use asyn apis, I need to use signal and slots, right?
                so if I use Qt::QueueConnection, my data will be processed in the gui thread, if the process need a lot of time, will it block my gui thread?
                or should I use Qt::DirectConnection?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #12

                @Mozzie
                Auto, you should use Qt::AutoConnection, which is the default one.

                There are very very few situations where you as the developer have to force a connection time. You shouldn't run into it normally.

                If data processing takes a long time, then yes, your Gui will be unresponsive. Thats where Threading would come into place then. But probably not the subclassing QThread method


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                MozzieM 1 Reply Last reply
                2
                • J.HilkJ J.Hilk

                  @Mozzie
                  Auto, you should use Qt::AutoConnection, which is the default one.

                  There are very very few situations where you as the developer have to force a connection time. You shouldn't run into it normally.

                  If data processing takes a long time, then yes, your Gui will be unresponsive. Thats where Threading would come into place then. But probably not the subclassing QThread method

                  MozzieM Offline
                  MozzieM Offline
                  Mozzie
                  wrote on last edited by
                  #13

                  @J-Hilk

                  thanks, but i still have some question.

                  as you said: Thats where Threading would come into place then.

                  so, I just need to use another thread to process my data?

                  and
                  even QNetWorkAccessManger is limited to 5 parallel get requests, It still can request multiple times in 1 second, so how to ensure that you can only request a few times a second by using async apis

                  J.HilkJ 1 Reply Last reply
                  0
                  • MozzieM Mozzie

                    @J-Hilk

                    thanks, but i still have some question.

                    as you said: Thats where Threading would come into place then.

                    so, I just need to use another thread to process my data?

                    and
                    even QNetWorkAccessManger is limited to 5 parallel get requests, It still can request multiple times in 1 second, so how to ensure that you can only request a few times a second by using async apis

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #14

                    @Mozzie said in How to use QNetworkAccessManager in multiple threads:

                    so, I just need to use another thread to process my data?

                    yes, if it's needed, does your processing really use 100+ ms of time (per requested data)?

                    even QNetWorkAccessManger is limited to 5 parallel get requests, It still can request multiple times in 1 second, so how to ensure that you can only request a few times a second by using async apis

                    yes they are processed one after the other, if you really do not want to automatically queue the requests, simply store your QNetworkReply pointers in a vector of size 5 and check (before the request) if any pointer is a nullptr


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    MozzieM 1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

                      @Mozzie said in How to use QNetworkAccessManager in multiple threads:

                      so, I just need to use another thread to process my data?

                      yes, if it's needed, does your processing really use 100+ ms of time (per requested data)?

                      even QNetWorkAccessManger is limited to 5 parallel get requests, It still can request multiple times in 1 second, so how to ensure that you can only request a few times a second by using async apis

                      yes they are processed one after the other, if you really do not want to automatically queue the requests, simply store your QNetworkReply pointers in a vector of size 5 and check (before the request) if any pointer is a nullptr

                      MozzieM Offline
                      MozzieM Offline
                      Mozzie
                      wrote on last edited by
                      #15

                      @J-Hilk
                      Thank you very much, It is really helpful.

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

                        Hi,

                        Just in case, currently it's 6 requests simultaneously.

                        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