diff options
| author | jvoisin | 2018-04-14 16:10:45 +0200 |
|---|---|---|
| committer | jvoisin | 2018-04-14 16:13:51 +0200 |
| commit | 6f4ed2490fbcde0b74e7b8251ad71e29b430b8ef (patch) | |
| tree | 50bfda50dae886697e683ade3c5cd3473fb27f17 | |
| parent | cef5068fe9427645e2e3edef7efe8cb83897f6d4 (diff) | |
Thread the cleaning process
| -rwxr-xr-x | main.py | 32 |
1 files changed, 29 insertions, 3 deletions
| @@ -3,6 +3,9 @@ | |||
| 3 | import os | 3 | import os |
| 4 | import mimetypes | 4 | import mimetypes |
| 5 | import argparse | 5 | import argparse |
| 6 | from threading import Thread | ||
| 7 | import multiprocessing | ||
| 8 | from queue import Queue | ||
| 6 | 9 | ||
| 7 | from src import parser_factory | 10 | from src import parser_factory |
| 8 | 11 | ||
| @@ -75,6 +78,14 @@ def __get_files_recursively(files): | |||
| 75 | for _f in _files: | 78 | for _f in _files: |
| 76 | yield os.path.join(path, _f) | 79 | yield os.path.join(path, _f) |
| 77 | 80 | ||
| 81 | def __do_clean_async(q): | ||
| 82 | while True: | ||
| 83 | f = q.get() | ||
| 84 | if f is None: # nothing more to process | ||
| 85 | return | ||
| 86 | clean_meta(f) | ||
| 87 | q.task_done() | ||
| 88 | |||
| 78 | 89 | ||
| 79 | def main(): | 90 | def main(): |
| 80 | arg_parser = create_arg_parser() | 91 | arg_parser = create_arg_parser() |
| @@ -86,12 +97,27 @@ def main(): | |||
| 86 | show_parsers() | 97 | show_parsers() |
| 87 | return | 98 | return |
| 88 | 99 | ||
| 89 | if args.show: | 100 | elif args.show: |
| 90 | for f in __get_files_recursively(args.files): | 101 | for f in __get_files_recursively(args.files): |
| 91 | show_meta(f) | 102 | show_meta(f) |
| 92 | else: | 103 | return |
| 104 | |||
| 105 | else: # Thread the cleaning | ||
| 106 | q = Queue(maxsize=0) | ||
| 107 | threads = list() | ||
| 93 | for f in __get_files_recursively(args.files): | 108 | for f in __get_files_recursively(args.files): |
| 94 | clean_meta(f) | 109 | q.put(f) |
| 110 | |||
| 111 | for _ in range(multiprocessing.cpu_count()): | ||
| 112 | worker = Thread(target=__do_clean_async, args=(q, )) | ||
| 113 | worker.start() | ||
| 114 | threads.append(worker) | ||
| 115 | |||
| 116 | for _ in range(multiprocessing.cpu_count()): | ||
| 117 | q.put(None) # stop the threads | ||
| 118 | |||
| 119 | for worker in threads: | ||
| 120 | worker.join() | ||
| 95 | 121 | ||
| 96 | 122 | ||
| 97 | if __name__ == '__main__': | 123 | if __name__ == '__main__': |
