Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can we make post request using QNetworkAccessManager in secondary thread?



  • I have multiple questions.

    • Can we make network request from secondary thread using QNetworkAccessManager ? Here we are making sure that the QNetworkAcessManager is created in secondary thread only.

    • I want to make multiple network request in my application. Among those request there are many blocking request which blocks the UI. So how can we achieve that?

    • Also in the documentation it is written that QNetworkAccessManager queues the QnetworkRequest by itself then how can we get the queued request count?


  • Lifetime Qt Champion

    hi
    1)
    Yes, you can use the worker approach
    https://wiki.qt.io/QThreads_general_usage
    and run the QNetworkAccessManager there as far as i know it should work fine.
    2)
    You can run 6 concurrent requests and handle that in an asynchronous manner. Using lambdas you can make the code very compact
    and local but its not blocking as that is not supported.

    There is a way to do it

    QNetworkAccessManager qnam;
    QNetworkReply *reply = qnam.get(QNetworkRequest(QUrl(…)));
    QEventLoop loop;
    QObject::connect(reply, SIGNAL (finished()), &loop, SLOT (quit()));
    loop.exec();
    

    But using QEventLoop all over the application is the road to odd side effects and not really recommended.

    So why must it be blocking ?
    Cant you just use the normal api and simply handle the requests when they come in ?
    Do you need more than 6 requests at the same time ?

    I have not seen any access to that info but check out the code and see if you are lucky :)
    https://code.woboq.org/qt5/qtbase/src/network/access/qnetworkaccessmanager.cpp.html


  • Lifetime Qt Champion

    hi
    1)
    Yes, you can use the worker approach
    https://wiki.qt.io/QThreads_general_usage
    and run the QNetworkAccessManager there as far as i know it should work fine.
    2)
    You can run 6 concurrent requests and handle that in an asynchronous manner. Using lambdas you can make the code very compact
    and local but its not blocking as that is not supported.

    There is a way to do it

    QNetworkAccessManager qnam;
    QNetworkReply *reply = qnam.get(QNetworkRequest(QUrl(…)));
    QEventLoop loop;
    QObject::connect(reply, SIGNAL (finished()), &loop, SLOT (quit()));
    loop.exec();
    

    But using QEventLoop all over the application is the road to odd side effects and not really recommended.

    So why must it be blocking ?
    Cant you just use the normal api and simply handle the requests when they come in ?
    Do you need more than 6 requests at the same time ?

    I have not seen any access to that info but check out the code and see if you are lucky :)
    https://code.woboq.org/qt5/qtbase/src/network/access/qnetworkaccessmanager.cpp.html


Log in to reply