parallelize creat media function call
This commit is contained in:
1 parent
86679e4e18
commit
8cccf674cf
1 file changed
+18
-8
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from __future__ import annotations
|
||||
from concurrent.futures import ProcessPoolExecutor, as_completed
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
@@ -170,6 +171,15 @@ def find_book_dirs(root: Path) -> tuple[list[Any], int]:
|
||||
|
||||
return sorted(directories), size
|
||||
|
||||
def process_book(args):
|
||||
target, book_dir = args
|
||||
try:
|
||||
create_m4b(target, book_dir)
|
||||
return book_dir, None
|
||||
except Exception as exc:
|
||||
return book_dir, str(exc)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
@@ -200,15 +210,15 @@ def main() -> None:
|
||||
if name != "y":
|
||||
log_message(f"Aborted")
|
||||
exit(0)
|
||||
size = 0
|
||||
|
||||
# Root folder containing many books
|
||||
for book_dir in book_dir_set:
|
||||
size += sum(file.stat().st_size for pattern in ("*.mp3", "*.m4a", "*.m4b") for file in book_dir.glob(pattern))
|
||||
print(f"{size / 1024**3:07.2f}/{library_size/ 1024**3:07.2f} - {size/library_size*100:.2f}% ... ",end="")
|
||||
try:
|
||||
create_m4b(target,book_dir)
|
||||
except Exception as exc:
|
||||
log_message(f"{RED}[ERROR]{RESET} {book_dir}: {exc}")
|
||||
with ProcessPoolExecutor(max_workers=8) as executor:
|
||||
for book_dir, error in executor.map(
|
||||
process_book,
|
||||
((target, book_dir) for book_dir in book_dir_set),
|
||||
):
|
||||
if error:
|
||||
log_message(f"{RED}[ERROR]{RESET} {book_dir}: {error}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user