Using itertools.cycle to commit occasionally.

This commit is contained in:
Roland 2022-10-09 13:12:59 -06:00
parent 9a47135c99
commit dad00ad065
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
2 changed files with 7 additions and 6 deletions

View file

@ -1,3 +1,4 @@
import itertools
import json
import pathlib
from abc import ABC
@ -62,7 +63,7 @@ def archive_modeler(groups: Dict[str, List[File]], session: Session):
if any(lib.is_singlefile_file(i.path) for i in group):
archive_groups[stem] = group
count = 0
count = itertools.cycle(range(101))
for stem, group in archive_groups.items():
readability_file, singlefile_file, readability_json_file, readability_txt_file, screenshot_file = \
match_archive_files(group)
@ -96,8 +97,7 @@ def archive_modeler(groups: Dict[str, List[File]], session: Session):
# Remove this group, it will not be processed by other modelers.
del groups[stem]
count += 1
if not count % 100:
if next(count) == 100:
# Commit occasionally.
session.commit()

View file

@ -1,3 +1,4 @@
import itertools
import json
import pathlib
from abc import ABC
@ -21,8 +22,9 @@ __all__ = ['video_modeler', 'VideoIndexer']
@register_modeler
def video_modeler(groups: Dict[str, List[File]], session: Session):
count = itertools.cycle(range(101))
# Only process groups that contain a video file.
count = 0
local_groups = groups.copy()
for stem, group in local_groups.items():
video_file = next((i for i in group if i.mimetype.split('/')[0] == 'video' and i.path.suffix != '.part'), None)
@ -57,8 +59,7 @@ def video_modeler(groups: Dict[str, List[File]], session: Session):
# Remove this group, it will not be processed by other modelers.
del groups[stem]
count += 1
if not count % 100:
if next(count) == 100:
# Commit occasionally.
session.commit()