Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Multithreading & GUI

  1. #1
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Multithreading & GUI

    Hello,

    I'm trying to develop a Desktop application that will send multiple request to a website and store the response. i want to do the requests in a separate threat so that the GUI will be responsive. I tried to use QNetworkAccessManager with run,qtconcurrent and i was not able to get success in achieving my goal. Can you guys give me some advice or ideas how can i implement multithreading with QNetworkAccessManager

    example algorithm:
    for(int i=0;i<1000;i++)
    {
    //send HTTP request using QNetworkAccessManager

    } // should not affect the gui !!!!!!

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Multithreading & GUI

    Have you tried to use QTimer with 0 interval ?
    Qt Code:
    1. for(int i=0;i<1000;i++)
    2. {
    3. //send HTTP request using QNetworkAccessManager
    4.  
    5. } // should not affect the gui !!!!!!
    6.  
    7. // with QTimer:
    8.  
    9. void MyClass::sendRequest(){
    10. // send request using QNAM
    11. ++_requestSent;
    12. if (_requestSent < 1000 && !_abort){
    13. QTimer::singleShot(0,this,SLOT(sendRequest()));
    14. }
    15. }
    16. // will not affect the gui, timeouts will be processed as soon as all gui events have been processed
    To copy to clipboard, switch view to plain text mode 
    Btw. why do you need to send a thousand http requests ?

  3. The following user says thank you to stampede for this useful post:

    qthread (30th July 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Thanks for helping . I need it for doing testing.

    The app crashed with this error message:" Creating pipes for GWakeup: Too many open files".(with interval 500) Is that because of too many network connections?! (i'm testing it against server running in my Virtual machine).

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Multithreading & GUI

    The app crashed with this error message:" Creating pipes for GWakeup: Too many open files".(with interval 500) Is that because of too many network connections?!
    Maybe it is the cause. Try to process next request when previous one finished (connect to QNetworkReply::finished () signal) instead of using a timer.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading & GUI

    Sending network requests does not "affect the GUI". You can safely do them in the main thread. Using multiple threads with QNetworkAccessManager makes little sense.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by stampede View Post
    Maybe it is the cause. Try to process next request when previous one finished (connect to QNetworkReply::finished () signal) instead of using a timer.
    I want to send multiple requests. I think using 'finished' method will slow down my task.

    Quote Originally Posted by wysota View Post
    Sending network requests does not "affect the GUI". You can safely do them in the main thread. Using multiple threads with QNetworkAccessManager makes little sense.
    Sending network requests wont affect the gui but running for loop will.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    I want to send multiple requests. I think using 'finished' method will slow down my task.
    Why so?

    Sending network requests wont affect the gui but running for loop will.
    What for loop?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    Why so?


    What for loop?
    If i use finished method, the next request have to wait for the previous one,right?!

    I need a loop for sending multiple request,right?!

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    If i use finished method, the next request have to wait for the previous one,right?!
    No, of course not. You schedule as many requests as you want and QNetworkAccessManager will execute them when the right time comes. As far as I remember it will be sending four requests at a time.

    I need a loop for sending multiple request,right?!
    No, but having the loop is ok as well. Scheduling 1000 requests should take just a couple of milliseconds.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    No, of course not. You schedule as many requests as you want and QNetworkAccessManager will execute them when the right time comes. As far as I remember it will be sending four requests at a time.



    No, but having the loop is ok as well. Scheduling 1000 requests should take just a couple of milliseconds.


    Ok. Thanks for the info. But how can i prevent the loop from affecting my gui. For some reason, i am not able to run the QNAM in separate thread. Can you give tell me steps or sample snippet for achieving my task

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    Ok. Thanks for the info. But how can i prevent the loop from affecting my gui.
    It will not affect your GUI.

    For some reason, i am not able to run the QNAM in separate thread.
    You are probably not running an event loop in that thread.

    Can you give tell me steps or sample snippet for achieving my task
    Sure, just state what the actual task is
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    It will not affect your GUI.


    You are probably not running an event loop in that thread.


    Sure, just state what the actual task is
    I am new to qt. I am not able to find any good resource in multithreading. I just want to do fuzzing on web applictions which i was assigned to do testing.

    Here is exactly what i wanted:
    I need to send more than 1000 or 10,000 requests to a web application without affecting the GUI(i tried to run 10,000 request in main thread, it's affecting the gui, not able to access any other items). so i wanted to run the QNAM &loop in a separate thread or any other way in such a way that it should not affect the other tasks. Can you give me a sample snippet or tutorial links to achieve this

    Thank you

  14. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    One way to do it is to have a worker object and move it to a QThread

    Qt Code:
    1. Worker *worker = new Worker(); // class derived from QObject
    2.  
    3. QThread *thread = new QThread();
    4. worker->moveToThread(thread);
    5. connect(thread, SIGNAL(started()), worker, SLOT(start())); // Worker needs a slot that start the work
    6. thread->start();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  15. The following user says thank you to anda_skoa for this useful post:

    qthread (3rd August 2014)

  16. #14
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by anda_skoa View Post
    One way to do it is to have a worker object and move it to a QThread

    Qt Code:
    1. Worker *worker = new Worker(); // class derived from QObject
    2.  
    3. QThread *thread = new QThread();
    4. worker->moveToThread(thread);
    5. connect(thread, SIGNAL(started()), worker, SLOT(start())); // Worker needs a slot that start the work
    6. thread->start();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Thanks it worked for me

    How can i distinguish the responses?! I'm sending 1000 requests.
    I can use response->request().url() function to get the corresponding url. However i want to set a unique number for each request and display along with corresponding response. How to do that in QNAM?

  17. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    One option would be to have a mapping from the QNetworkReply pointer to your reference data, e.g. using QHash.

    In your case where you only need a single number, it is probably quicker to just set this value as a dynamic property on the QNetworkReply object.
    See QObject::setProperty().

    Cheers,
    _

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading & GUI

    Just remember that QNetworkReply is a QIODevice which in turn is a QObject thus accessing it from more than one thread is not safe. Push the object to the main thread before you try accessing it. The original thread will probably not try accessing the object after finished() is emitted but it is better to be safe than sorry.

    Which of course doesn't negate the fact that there is probably no point in using any extra threads here..
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    I will be thankful if anyone give me a sample snippet to distinguish each QNAM response from others

  20. #18
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Each request is handled by a QNetworkReply object. So each request has a different, distinguishable, pointer to a QNetworkReply instance.
    Each instance has the QNetworkRequest that it was created for.

    Qt Code:
    1. void MyClass::networkRequestFinished(QNetworkReply *reply)
    2. {
    3. qDebug() << "Request for" << reply->request().url() << "finished" << (reply->error() != QNetworkReply::NoError ? "with an error" : "without error");
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  21. #19
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Thanks is it possible to pass a unique value to QNAM and access them via QNetworkReply other than URL ?!

  22. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    Thanks is it possible to pass a unique value to QNAM and access them via QNetworkReply other than URL ?!
    What kind of unique value?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Multithreading
    By havij000 in forum Newbie
    Replies: 22
    Last Post: 21st August 2013, 14:35
  2. When to use Multithreading?
    By "BumbleBee" in forum General Programming
    Replies: 5
    Last Post: 30th April 2011, 19:21
  3. regarding multithreading
    By mohanakrishnan in forum Qt Programming
    Replies: 19
    Last Post: 9th December 2009, 09:21
  4. multithreading
    By mickey in forum General Programming
    Replies: 2
    Last Post: 5th June 2008, 23:01
  5. MultiThreading in Qt
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 7th November 2006, 20:26

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.