Skip to content

Commit 3e52730

Browse files
author
wangwj-f
committed
批量结束任务时,效率改善
1 parent 6f3e803 commit 3e52730

File tree

2 files changed

+55
-84
lines changed

2 files changed

+55
-84
lines changed

samples/networktool/networktool.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include "networkreply.h"
2222

2323

24-
//#define TEST_PERFORMANCE
25-
#define TEST_PERFORMANCE_EXEC_COUNT 100
24+
//#define TEST_PERFORMANCE_NO_TASK_LIST
25+
#define BATCH_REQUEST_EXEC_COUNT 1
2626

2727
#define DEFAULT_CONCURRENT_TASK 8
2828
#define MAX_CONCURRENT_TASK 16
@@ -491,7 +491,7 @@ void NetworkTool::onDownload()
491491
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
492492
this, SLOT(onRequestFinished(const RequestTask &)));
493493

494-
#ifndef TEST_PERFORMANCE
494+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
495495
m_pListViewDoing->insert(QVariant::fromValue<RequestTask>(req));
496496
switchTaskView(true);
497497
#endif
@@ -536,7 +536,7 @@ void NetworkTool::onUpload()
536536
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
537537
this, SLOT(onRequestFinished(const RequestTask &)));
538538

539-
#ifndef TEST_PERFORMANCE
539+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
540540
m_pListViewDoing->insert(QVariant::fromValue<RequestTask>(req));
541541
switchTaskView(true);
542542
#endif
@@ -570,7 +570,7 @@ void NetworkTool::onGetRequest()
570570
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
571571
this, SLOT(onRequestFinished(const RequestTask &)));
572572

573-
#ifndef TEST_PERFORMANCE
573+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
574574
m_pListViewDoing->insert(QVariant::fromValue<RequestTask>(req));
575575
switchTaskView(true);
576576
#endif
@@ -614,7 +614,7 @@ void NetworkTool::onPostRequest()
614614
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
615615
this, SLOT(onRequestFinished(const RequestTask &)));
616616

617-
#ifndef TEST_PERFORMANCE
617+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
618618
m_pListViewDoing->insert(QVariant::fromValue<RequestTask>(req));
619619
switchTaskView(true);
620620
#endif
@@ -669,7 +669,7 @@ void NetworkTool::onPutRequest()
669669
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
670670
this, SLOT(onRequestFinished(const RequestTask &)));
671671

672-
#ifndef TEST_PERFORMANCE
672+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
673673
m_pListViewDoing->insert(QVariant::fromValue<RequestTask>(req));
674674
switchTaskView(true);
675675
#endif
@@ -703,7 +703,7 @@ void NetworkTool::onDeleteRequest()
703703
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
704704
this, SLOT(onRequestFinished(const RequestTask &)));
705705

706-
#ifndef TEST_PERFORMANCE
706+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
707707
m_pListViewDoing->insert(QVariant::fromValue<RequestTask>(req));
708708
switchTaskView(true);
709709
#endif
@@ -737,7 +737,7 @@ void NetworkTool::onHeadRequest()
737737
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
738738
this, SLOT(onRequestFinished(const RequestTask &)));
739739

740-
#ifndef TEST_PERFORMANCE
740+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
741741
m_pListViewDoing->insert(QVariant::fromValue<RequestTask>(req));
742742
switchTaskView(true);
743743
#endif
@@ -850,10 +850,8 @@ void NetworkTool::onBatchRequest()
850850
requests.append(std::move(req));
851851
}
852852

853-
#ifdef TEST_PERFORMANCE
854-
for (int i = 0; i < TEST_PERFORMANCE_EXEC_COUNT; ++i)
853+
for (int i = 0; i < BATCH_REQUEST_EXEC_COUNT; ++i)
855854
{
856-
#endif // #ifdef TEST_PERFORMANCE
857855

858856
quint64 batchId;
859857
NetworkReply *pReply = NetworkManager::globalInstance()->addBatchRequest(requests, batchId);
@@ -865,7 +863,7 @@ void NetworkTool::onBatchRequest()
865863
connect(pReply, SIGNAL(requestFinished(const RequestTask &)),
866864
this, SLOT(onRequestFinished(const RequestTask &)));
867865

868-
#ifndef TEST_PERFORMANCE
866+
#ifndef TEST_PERFORMANCE_NO_TASK_LIST
869867
QVector<QVariant> vec;
870868
vec.resize(requests.size());
871869
int i = 0;
@@ -878,10 +876,7 @@ void NetworkTool::onBatchRequest()
878876
switchTaskView(true);
879877
#endif
880878
}
881-
882-
#ifdef TEST_PERFORMANCE
883879
}
884-
#endif // #ifdef TEST_PERFORMANCE
885880
}
886881

887882
//request: 任务信息
@@ -919,12 +914,12 @@ void NetworkTool::onRequestFinished(const RequestTask &request)
919914
}
920915
}
921916

922-
//#ifndef TEST_PERFORMANCE
917+
//#ifndef TEST_PERFORMANCE_NO_TASK_LIST
923918
if (m_pListViewDoing)
924919
{
925920
m_pListViewDoing->onTaskFinished(request);
926921
}
927-
//#endif // !TEST_PERFORMANCE
922+
//#endif // !TEST_PERFORMANCE_NO_TASK_LIST
928923
}
929924

930925
void NetworkTool::onBatchDownloadProgress(quint64 batchId, qint64 bytes)

src/networkmanager.cpp

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ void NetworkManagerPrivate::stopRequest(quint64 uiTaskId)
213213

214214
{
215215
QMutexLocker locker(&m_mutex);
216+
reply = m_mapReply.take(uiTaskId);
216217

217218
if (m_mapRunnable.contains(uiTaskId))
218219
{
@@ -237,8 +238,6 @@ void NetworkManagerPrivate::stopRequest(quint64 uiTaskId)
237238
{
238239
m_queFailed.remove(uiTaskId);
239240
}
240-
241-
reply = m_mapReply.take(uiTaskId);
242241
}
243242

244243
if (reply.get())
@@ -258,10 +257,11 @@ void NetworkManagerPrivate::stopBatchRequests(quint64 uiBatchId)
258257

259258
{
260259
QMutexLocker locker(&m_mutex);
260+
reply = m_mapBatchReply.take(uiBatchId);
261261

262262
std::shared_ptr<NetworkRunnable> r = nullptr;
263263
//qDebug() << "Runnable[Before]: " << m_mapRunnable.size();
264-
for (auto iter = m_mapRunnable.cbegin(); iter != m_mapRunnable.cend(); ++iter)
264+
for (auto iter = m_mapRunnable.begin(); iter != m_mapRunnable.end();)
265265
{
266266
r = iter.value();
267267
if (r.get() && r->batchId() == uiBatchId)
@@ -275,39 +275,15 @@ void NetworkManagerPrivate::stopBatchRequests(quint64 uiBatchId)
275275
m_pThreadPool->cancel(r.get());
276276
r->quit();
277277
#endif
278+
iter = m_mapRunnable.erase(iter);
278279
}
279-
}
280-
281-
try
282-
{
283-
//之所以遍历两遍,是让runnable对象先退出,再让他析构
284-
for (auto iter = m_mapRunnable.begin(); iter != m_mapRunnable.end();)
280+
else
285281
{
286-
r = iter.value();
287-
if (r.get() && r->batchId() == uiBatchId)
288-
{
289-
iter = m_mapRunnable.erase(iter);
290-
}
291-
else
292-
{
293-
++iter;
294-
}
282+
++iter;
295283
}
296284
}
297-
catch (std::exception* e)
298-
{
299-
LOG_ERROR("erase batch runnable exception: " << e->what());
300-
qCritical() << "erase batch runnable exception:" << QString::fromUtf8(e->what());
301-
}
302-
catch (...)
303-
{
304-
LOG_ERROR("erase batch runnable unknown exception");
305-
qCritical() << "erase batch runnable unknown exception";
306-
}
307285
//qDebug() << "Runnable[After]: " << m_mapRunnable.size();
308286

309-
reply = m_mapBatchReply.take(uiBatchId);
310-
311287
if (m_mapBatchTotalSize.contains(uiBatchId))
312288
{
313289
m_mapBatchTotalSize.remove(uiBatchId);
@@ -348,53 +324,53 @@ void NetworkManagerPrivate::stopBatchRequests(quint64 uiBatchId)
348324

349325
void NetworkManagerPrivate::stopAllRequest()
350326
{
351-
if (!isStopAllState())
327+
if (isStopAllState())
328+
return;
329+
330+
markStopAllFlag();
331+
std::shared_ptr<NetworkReply> reply = nullptr;
332+
352333
{
353-
markStopAllFlag();
354-
std::shared_ptr<NetworkReply> reply = nullptr;
334+
QMutexLocker locker(&m_mutex);
335+
if (!m_mapReply.isEmpty())
336+
{
337+
reply = m_mapReply.last();
338+
}
339+
else if (!m_mapBatchReply.isEmpty())
340+
{
341+
reply = m_mapBatchReply.last();
342+
}
355343

344+
std::shared_ptr<NetworkRunnable> r = nullptr;
345+
for (auto iter = m_mapRunnable.cbegin(); iter != m_mapRunnable.cend(); ++iter)
356346
{
357-
QMutexLocker locker(&m_mutex);
358-
std::shared_ptr<NetworkRunnable> r = nullptr;
359-
for (auto iter = m_mapRunnable.cbegin(); iter != m_mapRunnable.cend(); ++iter)
347+
r = iter.value();
348+
if (r.get())
360349
{
361-
r = iter.value();
362-
if (r.get())
363-
{
364350
#if (QT_VERSION >= QT_VERSION_CHECK(5,9,0))
365-
if (!m_pThreadPool->tryTake(r.get()))
366-
{
367-
r->quit();
368-
}
369-
#else
370-
m_pThreadPool->cancel(r.get());
351+
if (!m_pThreadPool->tryTake(r.get()))
352+
{
371353
r->quit();
372-
#endif
373354
}
374-
}
375-
m_mapRunnable.clear();
376-
377-
if (!m_mapReply.isEmpty())
378-
{
379-
reply = m_mapReply.last();
380-
}
381-
else if (!m_mapBatchReply.isEmpty())
382-
{
383-
reply = m_mapBatchReply.last();
355+
#else
356+
m_pThreadPool->cancel(r.get());
357+
r->quit();
358+
#endif
384359
}
385360
}
386-
reset();
361+
m_mapRunnable.clear();
362+
}
363+
reset();
387364

388-
if (reply.get())
389-
{
390-
RequestTask t;
391-
t.uiId = RequestTask::ALL_TASK;
392-
t.bSuccess = false;
393-
t.bCancel = true;
394-
t.bytesContent = QString("Operation cancelled (All Request)").toUtf8();
365+
if (reply.get())
366+
{
367+
RequestTask t;
368+
t.uiId = RequestTask::ALL_TASK;
369+
t.bSuccess = false;
370+
t.bCancel = true;
371+
t.bytesContent = QString("Operation cancelled (All Request)").toUtf8();
395372

396-
reply->replyResult(t, true);
397-
}
373+
reply->replyResult(t, true);
398374
}
399375
}
400376

0 commit comments

Comments
 (0)