我知道在4.8版本中,每个http请求都有自己的线程来运行 我正在做一个链接检查应用程序,它在while循环中执行了很多http请求,我在Windows任务管理器中注意到我的应用程序随着时间的推移使用了超过1600个线程并且数量永远不会下降,直到它崩溃为止该应用程序。 (我猜这就是原因。)
我的问题是,QNetworkAccessManager
是否可以选择使用线程池?
或者它有完成其http请求后清除其线程的选项吗?
这是主循环:
while(!rpm_urlStack->isEmpty())
{
QString url = rpm_urlStack->top();
//define the reply
QNetworkReply *reply;
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);
// this is how I delete the reply object
delete 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));
}
}
}
答案 0 :(得分:1)
要使appears有效,必须在事件循环中调用deleteLater
方法,事件循环必须重新获得对执行的控制才能处理垃圾回收。
也许您应该重构代码以使事件循环替换您的while循环。或者,由于您没有使用finished
广告位来处理回复,也许您可以直接在RequestFinishedHandler
功能的末尾删除回复。