python-3.xmultiprocessingpython-multiprocessingmultiprocess

How to right use "Pool" in parallel downloading files?


I want use a parallel downloading videos from youtube, but my code ending with exception "PicklingError". Can you help guys with code, how it should be, please.

Another fixed variant:

import sys
#from pathos.multiprocessing import ProcessingPool as Pool
from multiprocessing import Pool
from pytube import YouTube
from youtubeMultiDownloader import UiMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog


class YouTubeInstance:
    def __init__(self, path):
        self.youtube = YouTube
        self.path = path
        #self.ui_obj = ui_obj

    def download_file(self, url):
        self.youtube(url).streams.get_highest_resolution().download(self.path)
        #self.ui.ui.youtube_outputs.setText(f'Video \'{self.youtube.title}\' has been downloaded successfully!')


class YouTubeMultiDownloader(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.pool = Pool
        self.ui = UiMainWindow()
        self.ui.setup_ui(self)
        self.path_to_dir = None
        self.urls = None

    def _get_urls_from_form(self):
        self.urls = self.ui.youtube_urls.toPlainText().split('\n')
        return len(self.urls)

    def choose_directory(self):
        self.path_to_dir = str(QFileDialog.getExistingDirectory(self, "Select Directory"))

    def run_multi_downloads(self):
        youtube = YouTubeInstance(self.path_to_dir)
        self.pool(self._get_urls_from_form()).map(youtube.download_file, self.urls)


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    application = YouTubeMultiDownloader()
    application.show()
    sys.exit(app.exec_())

Updated:

My ui :) My ui

Error 1 fixed: Error 1

Error 2 fixed: Error 2

Error 3 actual: Error 3


Solution

  • You've got the wrong side of the stick. Take a look at multiprocessing module documents. As it says, calling Pool method is for running multiple instance of same function simultaneously (in parallel). So call Pool method as many numbers you want, meanwhile your method does not any parameters, call it without any arguments:

    with Pool(5) as p:
        print(p.map(YouTubeMultiDownloader))
    

    It create 5 parallel instance. You can change the code an refine your errors.