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



  • 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);
    

    }@



  • 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();
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.