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. Im doing something wrong with QNetworkAccessManager thread request (the threads never get free)
Forum Updated to NodeBB v4.3 + New Features

Im doing something wrong with QNetworkAccessManager thread request (the threads never get free)

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.8k Views 1 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.
  • U Offline
    U Offline
    umen242
    wrote on last edited by
    #1

    i know that in version 4.8 each http request gets its own thread to run ,
    im doing links checker app that doing allot of http request in while loop
    i notice in the windows task manager that my app is using more then 1600 threads over time and the number never gos down only up until its crash the app . ( i guessing it is the couse ) my question is does QNetworkAccessManager has option to use thread pool ?
    or it has option to clean its thread after it finish its http request?

    this is the main loop :
    @while(!rpm_urlStack->isEmpty())
    {
    QString url = rpm_urlStack->top();
    rpm_urlStack->pop();

     QString urlForReq(url);
    
     bool returnVal = true;
     QNetworkRequest request;
    
     request.setUrl(QUrl(urlForReq));
     request.setRawHeader("User-Agent", USER_AGENT.toUtf8());
     request.setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
     request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
     request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
     request.setRawHeader("Connection", "Keep-Alive");
    
      QEventLoop loop;
      reply = m_networkManager->get(request);
      connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
      loop.exit();
      if(!loop.isRunning())
      {
    
         loop.exec();
    
      }
    
      RequestFinishedHandler(reply);
    

    }@

    1 Reply Last reply
    0
    • B Offline
      B Offline
      beemaster
      wrote on last edited by
      #2

      You can't do like this. You have to understand that signals are emitted ONLY after your function ends. Because event loop processing starts ONLY after your function ends. Look:

      @
      request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
      request.setRawHeader("Connection", "Keep-Alive");

      reply = m_networkManager->get(request);

      // Before you exit this method, you don't get any slots triggered from another thread.
      @

      You are doing it wrong with QEventLoop. It won't work! If you want blocking wait, you should do something like this:

      @
      request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8");
      request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
      request.setRawHeader("Connection", "Keep-Alive");

      reply = m_networkManager->get(request);

      if(reply->waitForReadyRead(-1)) {
      qDebug("It works!");
      QByteArray data = reply->readAll();
      QString rep(data);
      return rep;
      } else {
      qDebug("There must be an error");
      return reply->errorString();
      }
      @

      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