diff --git a/bazarr/api/subtitles/subtitles.py b/bazarr/api/subtitles/subtitles.py index f4b7657ad..b71d5ed7e 100644 --- a/bazarr/api/subtitles/subtitles.py +++ b/bazarr/api/subtitles/subtitles.py @@ -2,6 +2,7 @@ import os import sys +import gc from flask import request from flask_restful import Resource @@ -9,7 +10,7 @@ from flask_restful import Resource from database import TableEpisodes, TableMovies from helper import path_mappings from ..utils import authenticate -from subsyncer import subsync +from subsyncer import SubSyncer from utils import translate_subtitles_file, subtitles_apply_mods from list_subtitles import store_subtitles, store_subtitles_movie from config import settings @@ -46,6 +47,7 @@ class Subtitles(Resource): video_path = path_mappings.path_replace_movie(metadata['path']) if action == 'sync': + subsync = SubSyncer() if media_type == 'episode': subsync.sync(video_path=video_path, srt_path=subtitles_path, srt_lang=language, media_type='series', sonarr_series_id=metadata['sonarrSeriesId'], @@ -53,6 +55,8 @@ class Subtitles(Resource): else: subsync.sync(video_path=video_path, srt_path=subtitles_path, srt_lang=language, media_type='movies', radarr_id=id) + del subsync + gc.collect() elif action == 'translate': dest_language = language forced = True if request.form.get('forced') == 'true' else False diff --git a/bazarr/get_subtitle/sync.py b/bazarr/get_subtitle/sync.py index 6f341b17d..eb613dbcf 100644 --- a/bazarr/get_subtitle/sync.py +++ b/bazarr/get_subtitle/sync.py @@ -2,9 +2,10 @@ # fmt: off import logging +import gc from config import settings -from subsyncer import subsync +from subsyncer import SubSyncer def sync_subtitles(video_path, srt_path, srt_lang, forced, media_type, percent_score, sonarr_series_id=None, @@ -24,8 +25,11 @@ def sync_subtitles(video_path, srt_path, srt_lang, forced, media_type, percent_s subsync_threshold = settings.subsync.subsync_movie_threshold if not use_subsync_threshold or (use_subsync_threshold and percent_score < float(subsync_threshold)): + subsync = SubSyncer() subsync.sync(video_path=video_path, srt_path=srt_path, srt_lang=srt_lang, media_type=media_type, sonarr_series_id=sonarr_series_id, sonarr_episode_id=sonarr_episode_id, radarr_id=radarr_id) + del subsync + gc.collect() return True else: logging.debug("BAZARR subsync skipped because subtitles score isn't below this " diff --git a/bazarr/subsyncer.py b/bazarr/subsyncer.py index a9893b4c5..ac9a8489e 100644 --- a/bazarr/subsyncer.py +++ b/bazarr/subsyncer.py @@ -1,6 +1,5 @@ import logging import os -import gc from ffsubsync.ffsubsync import run, make_parser from utils import get_binary from utils import history_log, history_log_movie @@ -61,11 +60,8 @@ class SubSyncer: except Exception: logging.exception('BAZARR an exception occurs during the synchronization process for this subtitles: ' '{0}'.format(self.srtin)) - gc.collect() - return else: if settings.subsync.getboolean('debug'): - gc.collect() return result if os.path.isfile(self.srtout): if not settings.subsync.getboolean('debug'): @@ -89,9 +85,4 @@ class SubSyncer: else: logging.error('BAZARR unable to sync subtitles: {0}'.format(self.srtin)) - gc.collect() - return result - - -subsync = SubSyncer()