如何在并行下载文件时正确使用"Pool"?
How to right use "Pool" in parallel downloading files?
我想使用从 youtube 并行下载视频,但我的代码以异常“PicklingError
”结尾。你能帮忙写代码吗,请问代码应该如何。
另一个固定变体:
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_())
更新:
我的ui :)
错误 1 已修复:
错误 2 已修复:
实际错误 3:
你的立场错了。查看 multiprocessing
模块文档。正如它所说,调用 Pool
方法是针对 运行 同时(并行)相同函数的多个实例。所以调用 Pool
方法你想要多少数字,同时你的方法没有任何参数,不带任何参数调用它:
with Pool(5) as p:
print(p.map(YouTubeMultiDownloader))
创建5个并行实例。您可以更改代码以改进您的错误。
我想使用从 youtube 并行下载视频,但我的代码以异常“PicklingError
”结尾。你能帮忙写代码吗,请问代码应该如何。
另一个固定变体:
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_())
更新:
我的ui :)
错误 1 已修复:
错误 2 已修复:
实际错误 3:
你的立场错了。查看 multiprocessing
模块文档。正如它所说,调用 Pool
方法是针对 运行 同时(并行)相同函数的多个实例。所以调用 Pool
方法你想要多少数字,同时你的方法没有任何参数,不带任何参数调用它:
with Pool(5) as p:
print(p.map(YouTubeMultiDownloader))
创建5个并行实例。您可以更改代码以改进您的错误。