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