Raf*_*ues 5 python pickle multiprocessing python-3.x
这是我的代码
class MusicHandler(object):
""" Implements the logic to download musics """
def __init__(self):
""" Creates the class object, loads configs, absolute paths and create directories. Does not init download """
# create logger
self.logger = getLogger("music logger")
self.logger.info("Initializing MusicHandler class object")
# load config vars
self.configs = utils.get_configs()
# load absolute paths
self.dir_music = self.configs["music_path"]
self.dir_audio = utils.get_path_from_rel(self.configs["audio_path"], base_path=self.dir_music)
self.dir_video = utils.get_path_from_rel(self.configs["video_path"], base_path=self.dir_music)
self.taboo_path = utils.get_path_from_rel(self.configs["taboo_path"])
# make dir if not exists
list(map(utils.make_dir_safe, (self.dir_music, self.dir_audio, self.dir_video)))
@run_safe
def download_one(self, link, keep_video=False):
""" handles the download of one link. keep_video determines if video is saved or deleted. """
self.logger.info(f"Starting download of {link}")
if self.is_taboo(link):
self.logger.warning("Link is taboo, will skip it.")
return
name = self.download_video(link)
self.logger.info(f"Download of {name} (video) was a success.")
self.video_to_audio(name)
self.add_to_taboo(link)
if not keep_video:
self.remove_video(name)
self.logger.critical(f"Successfully downloaded {name}. Available at {self.dir_music}.")
def get_musics_linear(self, url_list):
""" Linear download of all links from list """
for link in url_list:
self.download_one(link)
@run_safe
def get_musics_parallel(self, url_list, num_procs=3):
""" Parallel download of all links from list """
with Pool(num_procs) as p:
print("here")
p.map(self.download_one, url_list)
print("there")
def is_taboo(self, link):
""" Verifies if link exists in the taboo file """
return utils.is_in_file(self.taboo_path, link)
def add_to_taboo(self, link):
""" Adds link to the taboo file """
utils.append_to_file(self.taboo_path, link)
def download_video(self, link):
""" Downloads the highest resolution video given a link. Returns file name """
yt = YouTube(link)
yt.streams.filter(progressive=True).order_by("resolution").desc().first().download(self.dir_video, yt.title)
return utils.safe_filename(yt.title)
def download_audio(self, link):
""" Downloads only the audio of a given link. Returns file name """
yt = YouTube(link)
yt.streams.filter(only_audio=True).first().download(self.dir_audio, yt.title)
return utils.safe_filename(yt.title)
def video_to_audio(self, name):
""" Converts a video to an audio file """
self.logger.info("Converting video to audio.")
video_f_path = os.path.join(self.dir_video, "{}{}".format(name, ".mp4"))
audio_f_path = os.path.join(self.dir_audio, "{}{}".format(name, ".wav"))
cmd_list = ["ffmpeg", "-i", video_f_path, "-f", "wav", "-ab", "19200", "-vn", audio_f_path]
FNULL = open(os.devnull, 'w')
subprocess.call(cmd_list, stdout=FNULL, stderr=subprocess.STDOUT)
def remove_video(self, name):
""" Deletes a video file """
self.logger.info("Removing video file.")
video_f_path = os.path.join(self.dir_video, "{}{}".format(name, ".mp4"))
if os.path.exists(video_f_path):
os.remove(video_f_path)Run Code Online (Sandbox Code Playgroud)
运行此代码时,出现以下错误:
文件“C:\Users\Y\PycharmProjects\pyMusic\lib\pyMusic.py”,第 143 行,在 get_musics_parallel p.map(self.download_one, url_list) 文件“C:\Program Files (x86)\Microsoft Visual Studio\ Shared\Python36_64\lib\multiprocessing\pool.py", line 266, in map return self._map_async(func, iterable, mapstar, chunksize).get() File "C:\Program Files (x86)\Microsoft Visual Studio\ Shared\Python36_64\lib\multiprocessing\pool.py", line 644, in get raise self._value File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\multiprocessing\pool.py",第 424 行,在 _handle_tasks put(task) 文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\multiprocessing\connection.py”中,第 206 行,发送 self._send_bytes(_ForkingPickler.dumps(obj)) 文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\multiprocessing\reduction.py”,第 51 行,在 dumps cls(buf, protocol).dump(obj) TypeError : 不能pickle _thread.RLock 对象
但是,如果我删除记录器,则代码可以毫无问题地执行。
知道为什么记录器会导致这个泡菜错误吗?错误发生在“get_musics_parallel”函数上,发生在 map(self.download_one, urllist) 行上。
我试图用谷歌搜索这个错误,但与这个泡菜错误相关的问题似乎与我的问题无关。
感谢您的帮助,亲切的问候
你不是在multiprocessing某处使用模块吗?它要求线程/进程之间交换的数据是可选择的。
记录器使用RLock哪个保存进程的状态,因此不能被酸洗。Logger 使用它,以便在整个脚本中获取多个记录器(或相同的记录器)时,它可以实现不弄乱日志文件(或任何其他日志输出)的魔力。
| 归档时间: |
|
| 查看次数: |
5995 次 |
| 最近记录: |