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
QtWS25 Last Chance

How to use QNetworkAccessManager in multiple threads

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 4.4k 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.
  • MozzieM Offline
    MozzieM Offline
    Mozzie
    wrote on last edited by
    #1

    hi,
    I want to use QNetworkAccessManager in multiple threads,
    my code is:

    
    void Test::run()
    {
    
    	qDebug() << __FUNCTION__;
    	QEventLoop loop;
    
    	QTimer timer;
    	timer.setSingleShot(true);
    
    	QNetworkRequest request;
    
    	request.setUrl(QUrl(this->link));
    
    	reply = networkManager->get(request);
    	reply->setReadBufferSize(0);
    	connect(reply, &QNetworkReply::readyRead, this, [this](){
    		QByteArray data = reply->readAll();
    		buffer.append(data);
    	}, Qt::DirectConnection);
    	connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
    			this, [this, &loop](QNetworkReply::NetworkError code){
    		loop.quit();
    	}, Qt::DirectConnection);
    	connect(reply, &QNetworkReply::sslErrors,
    			this, [this](const QList<QSslError> &errors){
    		for (auto error : errors) {
    			qWarning() << error.errorString();
    		}
    		reply->ignoreSslErrors();
    	}, Qt::DirectConnection);
    	
    	connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    	connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
    
    	timer.start(300000);
    	loop.exec();
    	timer.disconnect();
    	loop.disconnect();
    }
    

    but I got some output:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNetworkAccessManager(0x1a26fe50b80), parent's thread is QThread(0x1a270705560), current thread is QThread(0x1a270704ae0)
    

    so, What is the way to use QNetworkAccessManager in multiple threads?

    JonBJ CP71C 2 Replies Last reply
    0
    • MozzieM Mozzie

      hi,
      I want to use QNetworkAccessManager in multiple threads,
      my code is:

      
      void Test::run()
      {
      
      	qDebug() << __FUNCTION__;
      	QEventLoop loop;
      
      	QTimer timer;
      	timer.setSingleShot(true);
      
      	QNetworkRequest request;
      
      	request.setUrl(QUrl(this->link));
      
      	reply = networkManager->get(request);
      	reply->setReadBufferSize(0);
      	connect(reply, &QNetworkReply::readyRead, this, [this](){
      		QByteArray data = reply->readAll();
      		buffer.append(data);
      	}, Qt::DirectConnection);
      	connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
      			this, [this, &loop](QNetworkReply::NetworkError code){
      		loop.quit();
      	}, Qt::DirectConnection);
      	connect(reply, &QNetworkReply::sslErrors,
      			this, [this](const QList<QSslError> &errors){
      		for (auto error : errors) {
      			qWarning() << error.errorString();
      		}
      		reply->ignoreSslErrors();
      	}, Qt::DirectConnection);
      	
      	connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
      	connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
      
      	timer.start(300000);
      	loop.exec();
      	timer.disconnect();
      	loop.disconnect();
      }
      

      but I got some output:

      QObject: Cannot create children for a parent that is in a different thread.
      (Parent is QNetworkAccessManager(0x1a26fe50b80), parent's thread is QThread(0x1a270705560), current thread is QThread(0x1a270704ae0)
      

      so, What is the way to use QNetworkAccessManager in multiple threads?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Mozzie
      You can't, at least what you're trying to do. Have a read of e.g. https://forum.qt.io/topic/61436/use-qnetworkaccessmanager-from-multiple-threads, or search for qnetworkaccessmanager multiple threads.

      1 Reply Last reply
      2
      • MozzieM Mozzie

        hi,
        I want to use QNetworkAccessManager in multiple threads,
        my code is:

        
        void Test::run()
        {
        
        	qDebug() << __FUNCTION__;
        	QEventLoop loop;
        
        	QTimer timer;
        	timer.setSingleShot(true);
        
        	QNetworkRequest request;
        
        	request.setUrl(QUrl(this->link));
        
        	reply = networkManager->get(request);
        	reply->setReadBufferSize(0);
        	connect(reply, &QNetworkReply::readyRead, this, [this](){
        		QByteArray data = reply->readAll();
        		buffer.append(data);
        	}, Qt::DirectConnection);
        	connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
        			this, [this, &loop](QNetworkReply::NetworkError code){
        		loop.quit();
        	}, Qt::DirectConnection);
        	connect(reply, &QNetworkReply::sslErrors,
        			this, [this](const QList<QSslError> &errors){
        		for (auto error : errors) {
        			qWarning() << error.errorString();
        		}
        		reply->ignoreSslErrors();
        	}, Qt::DirectConnection);
        	
        	connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        	connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
        
        	timer.start(300000);
        	loop.exec();
        	timer.disconnect();
        	loop.disconnect();
        }
        

        but I got some output:

        QObject: Cannot create children for a parent that is in a different thread.
        (Parent is QNetworkAccessManager(0x1a26fe50b80), parent's thread is QThread(0x1a270705560), current thread is QThread(0x1a270704ae0)
        

        so, What is the way to use QNetworkAccessManager in multiple threads?

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

        @Mozzie
        Hi.
        I think the problem isn't the QNetworkAccessManager in QThread but where you have declared it in the QThread.
        I guess QNetworkAccessManager is a member of thread or you have done new of it in the constructor.
        I have seen this issue with other objects ... try to make a new of QNetworkAccessManager in run function.

        MozzieM 1 Reply Last reply
        3
        • CP71C CP71

          @Mozzie
          Hi.
          I think the problem isn't the QNetworkAccessManager in QThread but where you have declared it in the QThread.
          I guess QNetworkAccessManager is a member of thread or you have done new of it in the constructor.
          I have seen this issue with other objects ... try to make a new of QNetworkAccessManager in run function.

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

          @CP71

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

          jsulmJ CP71C 2 Replies Last reply
          0
          • MozzieM Mozzie

            @CP71

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

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

            MozzieM 1 Reply Last reply
            1
            • 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 Offline
                      jsulmJ Offline
                      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