pythonpyqtpyqt5qnetworkaccessmanagerqnetworkrequest

How do I wait for two asynchronous downloads to finish via QNetworkAccessManager?


=== 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_()

Solution

  • You have to create a class that tracks downloads:

    from dataclasses import dataclass
    import sys
    
    from PyQt5.QtCore import pyqtSignal, QObject, QUrl, QFile, QIODevice
    from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
    from PyQt5.QtWidgets import QApplication, QWidget
    
    
    @dataclass
    class DownloadRequest:
        url: QUrl
        filename: str
    
    
    class DownloadManager(QObject):
        finished = pyqtSignal()
    
        def __init__(self, parent=None):
            super().__init__(parent)
            self._manager = QNetworkAccessManager()
            self._pending_downloads = 0
    
            self.manager.finished.connect(self.handle_finished)
    
        @property
        def manager(self):
            return self._manager
    
        @property
        def pending_downloads(self):
            return self._pending_downloads
    
        def download(self, requests):
            for request in requests:
                qrequest = QNetworkRequest(request.url)
                qrequest.setAttribute(QNetworkRequest.User, request.filename)
                self.manager.get(qrequest)
                self._pending_downloads += 1
    
        def handle_finished(self, reply):
            self._pending_downloads -= 1
            if reply.error() != QNetworkReply.NoError:
                print(f"code: {reply.error()} message: {reply.errorString()}")
            else:
                print("successful")
                filename = reply.attribute(QNetworkRequest.User)
                file = QFile(filename)
                if file.open(QIODevice.WriteOnly):
                    file.write(reply.readAll())
    
            print(f"pending downloads {self.pending_downloads}")
            if self.pending_downloads == 0:
                self.finished.emit()
    
    
    class MainWindow(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self._manager = DownloadManager()
            self._manager.finished.connect(self.handle_finished)
    
            img_url1 = "https://docs.python.org/3/_static/py.png"
            img_url2 = "https://somesite.com/image2.jpg"
    
            request1 = DownloadRequest(QUrl(img_url1), img_url1.split("/")[-1])
            request2 = DownloadRequest(QUrl(img_url2), img_url2.split("/")[-1])
    
            self._manager.download([request1, request2])
    
        def handle_finished(self):
            print("finished")
    
    
    def main():
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        app.exec_()
    
    
    if __name__ == "__main__":
        main()