How do you define a request that a QNetworkReply signal is issued in response to when you are making multiple requests. In QtNetwork?

I have a project that will load an HTTP page, parse it and then open other pages based on the data received from the first page.

Since Qt QNetworkAccessManager works asyncronusly, it seems that I should be able to load more than one page at a time while continuing to make HTTP requests, and then the response will be taken care of in the order in which the responses are returned and will be processed in an even cycle.

I have a few problems figuring out how to do this:

At first I read somewhere in stackoverflow that you should use only one QNetworkAccess manager. I do not know if this is true.

The problem is that I am connecting to a ready-made slot in one QNetworkAccess manager. If I make several requests at the same time, I don’t know which request the ready signal answers. I don’t know if there is a way to check the QNetworkReply object, which is transmitted from the signal, to find out which answer it answers? Or should I use a different QNetworkAccessManager for each request?

Here is an example of how I am now clinging to things. But I know that this will not work when I make several requests during:

from PyQt4 import QtCore,QtGui,QtNetwork class Example(QtCore.QObject): def __init__(self): super().__init__() self.QNetworkAccessManager_1 = QtNetwork.QNetworkAccessManager() self.QNetworkCookieJar_1 = QtNetwork.QNetworkCookieJar() self.QNetworkAccessManager_1.setCookieJar(self.QNetworkCookieJar_1) self.app = QtGui.QApplication([]) def start_request(self): QUrl_1 = QtCore.QUrl('https://erikbandersen.com/') QNetworkRequest_1 = QtNetwork.QNetworkRequest(QUrl_1) # self.QNetworkAccessManager_1.finished.connect(self.someurl_finshed) self.QNetworkAccessManager_1.get(QNetworkRequest_1) def someurl_finshed(self, NetworkReply): # I do this so that this function won't get called for a diffent request # But it will only work if I'm doing one request at a time self.QNetworkAccessManager_1.finished.disconnect(self.someurl_finshed) page = bytes(NetworkReply.readAll()) # Do something with it print(page) QUrl_1 = QtCore.QUrl('https://erikbandersen.com/ipv6/') QNetworkRequest_1 = QtNetwork.QNetworkRequest(QUrl_1) # self.QNetworkAccessManager_1.finished.connect(self.someurl2_finshed) self.QNetworkAccessManager_1.get(QNetworkRequest_1) def someurl2_finshed(self, NetworkReply): page = bytes(NetworkReply.readAll()) # Do something with it print(page) kls = Example() kls.start_request() 
+4
source share
1 answer

I am not familiar with PyQt, but from a common Qt programming point

  • Using only one QNetworkAccessManager is the right design choice
  • The finished signal provides QNetworkReply* , with this we can identify the corresponding request using request () .

I hope this solves your problem with one manager and multiple requests.

This is a C ++ example that does the same.

+5
source

Source: https://habr.com/ru/post/1490085/


All Articles