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
Servers for Qt installer are currently down

How to use QNetworkAccessManager in multiple threads

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 4.4k 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.
  • M Offline
    M Offline
    Mozzie
    wrote on 15 May 2020, 08:42 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?

    J C 2 Replies Last reply 15 May 2020, 08:50
    0
    • M Mozzie
      15 May 2020, 08:42

      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?

      J Online
      J Online
      JonB
      wrote on 15 May 2020, 08:50 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
      • M Mozzie
        15 May 2020, 08:42

        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?

        C Offline
        C Offline
        CP71
        wrote on 15 May 2020, 08:55 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.

        M 1 Reply Last reply 15 May 2020, 09:06
        3
        • C CP71
          15 May 2020, 08:55

          @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.

          M Offline
          M Offline
          Mozzie
          wrote on 15 May 2020, 09:06 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?

          J C 2 Replies Last reply 15 May 2020, 09:10
          0
          • M Mozzie
            15 May 2020, 09:06

            @CP71

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

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 15 May 2020, 09:10 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

            M 1 Reply Last reply 15 May 2020, 09:27
            1
            • M Mozzie
              15 May 2020, 09:06

              @CP71

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

              C Offline
              C Offline
              CP71
              wrote on 15 May 2020, 09:16 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"

              M 1 Reply Last reply 15 May 2020, 09:27
              0
              • J jsulm
                15 May 2020, 09:10

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

                M Offline
                M Offline
                Mozzie
                wrote on 15 May 2020, 09:27 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 J 2 Replies Last reply 15 May 2020, 09:31
                0
                • C CP71
                  15 May 2020, 09:16

                  @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"

                  M Offline
                  M Offline
                  Mozzie
                  wrote on 15 May 2020, 09:27 last edited by
                  #8

                  @CP71
                  ok, thanks

                  1 Reply Last reply
                  1
                  • M Mozzie
                    15 May 2020, 09:27

                    @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 Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 15 May 2020, 09:31 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.

                    M 1 Reply Last reply 15 May 2020, 09:42
                    5
                    • M Mozzie
                      15 May 2020, 09:27

                      @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 Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 15 May 2020, 09:32 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 J.Hilk
                        15 May 2020, 09:31

                        @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]

                        M Offline
                        M Offline
                        Mozzie
                        wrote on 15 May 2020, 09:42 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 1 Reply Last reply 15 May 2020, 09:49
                        0
                        • M Mozzie
                          15 May 2020, 09:42

                          @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 Offline
                          J Offline
                          J.Hilk
                          Moderators
                          wrote on 15 May 2020, 09:49 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.

                          M 1 Reply Last reply 15 May 2020, 10:08
                          2
                          • J J.Hilk
                            15 May 2020, 09:49

                            @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

                            M Offline
                            M Offline
                            Mozzie
                            wrote on 15 May 2020, 10:08 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 1 Reply Last reply 15 May 2020, 11:22
                            0
                            • M Mozzie
                              15 May 2020, 10:08

                              @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 Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 15 May 2020, 11:22 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.

                              M 1 Reply Last reply 16 May 2020, 00:53
                              2
                              • J J.Hilk
                                15 May 2020, 11:22

                                @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

                                M Offline
                                M Offline
                                Mozzie
                                wrote on 16 May 2020, 00:53 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 16 May 2020, 14:48 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

                                  2/16

                                  15 May 2020, 08:50

                                  topic:navigator.unread, 14
                                  • Login

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