=== SUMMARY ===========================================
I use QNetworkAccessManager and QNetworkRequests to download two images simultaneously. How can I know for sure that two downloads finished?
=== DETAILED DESCRITION ===============================
I have URLs of two images and I want to download them asynchronously. To do that I initialize QNetworkAccessManager and use two QNetworkRequests. When finished, each request writes the contents of an image into a file.
The problem is that both requests know nothing of each other and, therefore, cannot verify whether the other one is finished.
Could you please tell me how can I wait for both requests to finish?
Here is the complete code:
import sys
from PyQt5.QtCore import QUrl, QFile, QIODevice
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
from PyQt5.QtWidgets import QApplication, QWidget
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
img_url1 = "https://somesite.com/image1.jpg"
img_url2 = "https://somesite.com/image2.jpg"
self.downloader = QNetworkAccessManager()
self.requests = []
self.temp_files = []
for index, mediafile_url in enumerate([img_url1, img_url2]):
self.requests.append(self.downloader.get(QNetworkRequest(QUrl(mediafile_url))))
request = self.requests[index]
self.temp_files.append(QFile(f'{mediafile_url.split("/")[-1]}'))
temp_file = self.temp_files[index]
request.finished.connect(lambda *args, r=request, tf=temp_file: self.download_image(r, tf))
self.show()
@staticmethod
def download_image(request, temp_file):
image_data = request.readAll()
temp_file.open(QIODevice.WriteOnly)
temp_file.write(image_data)
temp_file.close()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
You have to create a class that tracks downloads: