PDA

View Full Version : im doing something wrong with QNetworkAccessManager thread request free threads



umen
6th November 2012, 21:56
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);

}

RequestFinishedHandler(QNetworkReply *reply)
{


if (reply->error() > 0) {
QNetworkReply::NetworkError networkError = reply->error();
QString err = reply->errorString();


}
else {
QVariant vStatusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );
QMutexLocker lock(_pMutex); // _pMutex defined as class member
char *buffer;
buffer = getCurrentDateTime();
QTextStream out(m_file);
out << buffer <<" "<< _sCurrentUrl << "\n";
lock.unlock();
if(vStatusCodeV.toInt() == 200)
{
QString ApiResponse;
QByteArray data;
data=reply->readAll();
ApiResponse.append(QString::fromUtf8(data));

}

}
reply->deleteLater();

}

ChrisW67
6th November 2012, 22:29
Your code never returns to the event loop so the QNetworkReply objects will not be deleted until the entire queue is finished

I really do not know why you are trying to build a blocking system rather than simply connect the QNetworkAccessManager::finished() signal to a slot to handle the finished reply. The "problem" would then be a non-issue. I also see no reason for the mutex in the RequestFinishedHandler: yes, QNetworkAccessManager uses threads internally, but your code remains single-threaded.

umen
7th November 2012, 04:29
hello and thanks for the replay ,
im using eventloop and blocking system , because the flow of the app depend on the return of the http requests , i can just make a sync requests
and see what happen .
more info is that each such while loop is happening inside worker thread and i have 10 like this in each given moment , this is why i have the mutex.
they all writing to the same single file.

about the single threaded i do see 1600++ threads created from my app in the window manager , i guess is from the http requests .
but the problem is they never cleaned.
again as the topic says maybe im doing it all wrong .

ChrisW67
7th November 2012, 09:02
They are not cleaned up because the QNetworkReply objects are never being deleted.

umen
7th November 2012, 09:14
where do you suggest to clean them ? is it by simple
delete reply