From 457d38b19b73ad061d9563e5b01d4afab8bed5bc Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Thu, 19 Nov 2020 11:24:15 +0100 Subject: [PATCH] option for workers --- README.md | 7 +++++-- chkbit/main.py | 30 +++++++++++++++++++++++++----- setup.py | 2 +- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0d9ee26..d6ba483 100644 --- a/README.md +++ b/README.md @@ -45,18 +45,19 @@ chkbit will Run `chkbit PATH` to verify only. ``` -usage: chkbit.py [-h] [-u] [-f] [-i] [-q] [-v] [PATH [PATH ...]] +usage: chkbit.py [-h] [-u] [-f] [-i] [-w N] [-q] [-v] [PATH [PATH ...]] Checks the data integrity of your files. See https://github.com/laktak/chkbit-py positional arguments: - PATH + PATH directories to check optional arguments: -h, --help show this help message and exit -u, --update update indices (without this chkbit will only verify files) -f, --force force update of damaged items -i, --verify-index verify files in the index only (will not report new files) + -w N, --workers N number of workers to use, default=5 -q, --quiet quiet, don't show progress/information -v, --verbose verbose output @@ -71,6 +72,8 @@ Status codes: EXC: internal exception ``` +chkbit is set to use only 5 workers by default so it will not slow your system to a crawl. You can specify a higher number to make it a lot faster (requires about 128kB of memory per worker). + ## Repair chkbit cannot repair damage, its job is simply to detect it. diff --git a/chkbit/main.py b/chkbit/main.py index 3b3a025..4cdc401 100644 --- a/chkbit/main.py +++ b/chkbit/main.py @@ -51,7 +51,10 @@ class Main: epilog=STATUS_CODES, formatter_class=argparse.RawDescriptionHelpFormatter, ) - parser.add_argument("PATH", nargs="*") + + parser.add_argument( + "paths", metavar="PATH", type=str, nargs="*", help="directories to check" + ) parser.add_argument( "-u", @@ -71,6 +74,16 @@ class Main: help="verify files in the index only (will not report new files)", ) + parser.add_argument( + "-w", + "--workers", + metavar="N", + action="store", + type=int, + default=5, + help="number of workers to use, default=5", + ) + parser.add_argument( "-q", "--quiet", @@ -84,7 +97,7 @@ class Main: self.args = parser.parse_args() self.verbose = self.args.verbose self.quiet = self.args.quiet - if not self.args.PATH: + if not self.args.paths: parser.print_help() def _res_worker(self): @@ -98,15 +111,22 @@ class Main: def process(self): self.res_queue = queue.Queue() + + # the todo queue is used to distribute the work + # to the index threads todo_queue = queue.Queue() - for path in self.args.PATH: + # put the initial paths into the queue + for path in self.args.paths: todo_queue.put(path) + # start indexing workers = [ - IndexThread(idx, self.args, self.res_queue, todo_queue) for idx in range(5) + IndexThread(idx, self.args, self.res_queue, todo_queue) + for idx in range(self.args.workers) ] + # log the results from the workers res_worker = threading.Thread(target=self._res_worker) res_worker.daemon = True res_worker.start() @@ -142,7 +162,7 @@ class Main: def main(): try: m = Main() - if m.args.PATH: + if m.args.paths: m.process() m.print_result() except KeyboardInterrupt: diff --git a/setup.py b/setup.py index b08bc8b..f41117a 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ with open(os.path.join(os.path.dirname(__file__), "README.md"), encoding="utf-8" setup( name="chkbit", - version="2.1.0", + version="2.1.1", url="https://github.com/laktak/chkbit-py", author="Christian Zangl", author_email="laktak@cdak.net",