Results 1 to 8 of 8

Thread: Synchronous HTTP requests and QEventLoop

  1. #1
    Join Date
    Oct 2010
    Posts
    1
    Qt products
    Qt4

    Default Synchronous HTTP requests and QEventLoop

    First of all I'm trying perform synchronous HTTP requests. QNetworkAccessManager doesn't seem to offer support for it, so I'm trying to emulate this with a separate event loop.

    Qt Code:
    1. QNetworkAccessManager NAManager;
    2. QUrl url ("http://www.google.com");
    3. QNetworkRequest request(url);
    4. QNetworkReply *reply = NAManager.get(request);
    5. QEventLoop eventLoop;
    6. QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    7. eventLoop.exec();
    8. std::cout << "finished" << std::endl; //request finished here
    To copy to clipboard, switch view to plain text mode 

    Question: is there a nicer way to achieve this?

    Another question: is it possible to create an event loop that allows you to manually post events to it?

  2. #2
    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: Synchronous HTTP requests and QEventLoop

    Quote Originally Posted by bssldr View Post
    Question: is there a nicer way to achieve this?
    Apart from wrapping it all in a single call, no.

    Another question: is it possible to create an event loop that allows you to manually post events to it?
    You don't post events to event loops but rather to objects. QEventLoop is such an object too of course but I can't think of a use case where you'd want to post an event to an event loop object.
    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.


  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Synchronous HTTP requests and QEventLoop

    I can't think of a way thats nicer from what you want to do.

    QNetworkAccessManager was designed to be asynchronous. Why anyone would want a synchronous version is a little odd to me, but to each there own.

  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: Synchronous HTTP requests and QEventLoop

    Here is an example that uses a synchronous request.

    A system can issue requests to services X, Y, and Z over the network at user request. If this is the first time in a program run that an operation is requested then the system must first request a "login" (L) of the user with the service provider before completing the operation X, Y, or Z. L must complete entirely before operation X, Y, or Z is continued because a cookie returned by L must be available during X, Y, or Z. If the operation X, Y, or Z fails because the cookie has expired or been otherwise rejected by the service provider (perhaps they reboot a server) then the L process must be completed followed by a repeated X, Y, or Z.

    The L process looks like a classic synchronous request. What is the cleanest wholly asynchronous way to achieve this?

  5. #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: Synchronous HTTP requests and QEventLoop

    Quote Originally Posted by ChrisW67 View Post
    What is the cleanest wholly asynchronous way to achieve this?
    I'd say a state machine plus a signal emission upon detection of a failed request due to session timeout. It's nothing difficult but I agree there are situations when doing things synchronously simplifies matters greatly.
    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.


  6. #6
    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: Synchronous HTTP requests and QEventLoop

    Good Morning Wysota,

    Do you ever sleep?

    I've just stocked up on caffeine to explore the state machine machine approach to something like the the scenario above with few extra quirks. I've not used QStateMachine in anger so this should be an interesting morning.

  7. #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: Synchronous HTTP requests and QEventLoop

    Quote Originally Posted by ChrisW67 View Post
    Good Morning Wysota,
    Good morning

    Do you ever sleep?
    If I don't suffer from insomnia or don't have deadlines for the next day then in general - yes.

    I've just stocked up on caffeine to explore the state machine machine approach to something like the the scenario above with few extra quirks. I've not used QStateMachine in anger so this should be an interesting morning.
    By saying "state machine" I didn't exactly mean QStateMachine although of course it can be used here as well, it's just sometimes it's an overkill to use it when a simple enum variable holding the state would suffice
    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.


  8. #8
    Join Date
    Mar 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Smile Re: Synchronous HTTP requests and QEventLoop

    After years, but maybe helpfull. It is possible and makes possible to have one function with RETURN data. You can call it like:
    Qt Code:
    1. QByteaArray array = get_data( QUrl url );
    To copy to clipboard, switch view to plain text mode 

    so very nice

    this works fine for me:

    Qt Code:
    1. QByteArray gsm::data_request( QUrl url ){
    2. QNetworkReply* reply;
    3. QEventLoop connection_loop;
    4. connect(networkManager, SIGNAL( finished( QNetworkReply* ) ), &connection_loop, SLOT( quit() ) );
    5. networkRequest.setUrl( url );
    6. qDebug() << "reaching url: " << url;
    7. reply = networkManager->get( networkRequest );
    8. connection_loop.exec();
    9. qDebug() << "get -> " << url << ", done, size: " << reply->bytesAvailable();
    10. reply->deleteLater();
    11. return reply->readAll();
    12. }
    To copy to clipboard, switch view to plain text mode 

    so, it looks like you overloaded signal.
    Last edited by pcman11; 29th March 2017 at 22:00.

Similar Threads

  1. How to use QEventLoop?
    By MorrisLiang in forum Newbie
    Replies: 3
    Last Post: 13th May 2010, 16:23
  2. qprocess or qeventloop
    By knishaq in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2009, 10:14
  3. Synchronous QFtp?
    By aarpon in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2009, 09:28
  4. problem with Synchronous Http request
    By rchaitanya in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 30th January 2009, 11:03
  5. Synchronous Http (Good reason)
    By umbrella in forum Qt Programming
    Replies: 5
    Last Post: 4th April 2008, 14:14

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.