Results 1 to 5 of 5

Thread: im doing something wrong with QNetworkAccessManager thread request free threads

  1. #1
    Join Date
    May 2009
    Posts
    83

    Default im doing something wrong with QNetworkAccessManager thread request free threads

    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 :

    Qt Code:
    1. while(!rpm_urlStack->isEmpty())
    2. {
    3. QString url = rpm_urlStack->top();
    4. rpm_urlStack->pop();
    5.  
    6. QString urlForReq(url);
    7.  
    8. bool returnVal = true;
    9. QNetworkRequest request;
    10.  
    11. request.setUrl(QUrl(urlForReq));
    12. request.setRawHeader("User-Agent", USER_AGENT.toUtf8());
    13. request.setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    14. request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    15. request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
    16. request.setRawHeader("Connection", "Keep-Alive");
    17.  
    18. QEventLoop loop;
    19. reply = m_networkManager->get(request);
    20. connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    21. loop.exit();
    22. if(!loop.isRunning())
    23. {
    24.  
    25. loop.exec();
    26.  
    27. }
    28.  
    29. RequestFinishedHandler(reply);
    30.  
    31. }
    32.  
    33. RequestFinishedHandler(QNetworkReply *reply)
    34. {
    35.  
    36.  
    37. if (reply->error() > 0) {
    38. QNetworkReply::NetworkError networkError = reply->error();
    39. QString err = reply->errorString();
    40.  
    41.  
    42. }
    43. else {
    44. QVariant vStatusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    45. QMutexLocker lock(_pMutex); // _pMutex defined as class member
    46. char *buffer;
    47. buffer = getCurrentDateTime();
    48. QTextStream out(m_file);
    49. out << buffer <<" "<< _sCurrentUrl << "\n";
    50. lock.unlock();
    51. if(vStatusCodeV.toInt() == 200)
    52. {
    53. QString ApiResponse;
    54. QByteArray data;
    55. data=reply->readAll();
    56. ApiResponse.append(QString::fromUtf8(data));
    57.  
    58. }
    59.  
    60. }
    61. reply->deleteLater();
    62.  
    63. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: im doing something wrong with QNetworkAccessManager thread request free threads

    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    May 2009
    Posts
    83

    Default Re: im doing something wrong with QNetworkAccessManager thread request free threads

    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 .

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: im doing something wrong with QNetworkAccessManager thread request free threads

    They are not cleaned up because the QNetworkReply objects are never being deleted.

  5. #5
    Join Date
    May 2009
    Posts
    83

    Default Re: im doing something wrong with QNetworkAccessManager thread request free threads

    where do you suggest to clean them ? is it by simple
    delete reply

Similar Threads

  1. QNetworkAccessManager in multiple threads
    By mentalmushroom in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2013, 23:14
  2. Replies: 0
    Last Post: 21st April 2012, 11:07
  3. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 21:56
  4. sending soap request using QNetworkAccessManager question
    By babymonsta in forum Qt Programming
    Replies: 10
    Last Post: 21st April 2010, 07:55
  5. QNetworkAccessManager get request causing QSslSocket errors
    By Runtime Technologies in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2009, 22:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.