aboutsummaryrefslogtreecommitdiffhomepage
path: root/sec_certs/parallel_processing.py
diff options
context:
space:
mode:
authorAdam Janovsky2021-05-13 18:04:54 +0200
committerAdam Janovsky2021-05-13 18:04:54 +0200
commita910f54e314109203f38cea6a36b6ccc57587998 (patch)
tree569b3a8b7fc253888140c158a136abadc79fcb50 /sec_certs/parallel_processing.py
parentcac770c3662a826f545c47f6e2181b03139ed3f8 (diff)
downloadsec-certs-a910f54e314109203f38cea6a36b6ccc57587998.tar.gz
sec-certs-a910f54e314109203f38cea6a36b6ccc57587998.tar.zst
sec-certs-a910f54e314109203f38cea6a36b6ccc57587998.zip
delete old API
Diffstat (limited to 'sec_certs/parallel_processing.py')
-rw-r--r--sec_certs/parallel_processing.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/sec_certs/parallel_processing.py b/sec_certs/parallel_processing.py
new file mode 100644
index 00000000..b5519aab
--- /dev/null
+++ b/sec_certs/parallel_processing.py
@@ -0,0 +1,31 @@
+from tqdm import tqdm
+from multiprocessing.pool import Pool, ThreadPool
+from typing import Callable, Iterable, Optional
+import time
+
+
+def process_parallel(func: Callable, items: Iterable, max_workers: int, callback: Optional[Callable] = None,
+ use_threading: bool = True, progress_bar: bool = True, unpack: bool = False):
+ if use_threading is True:
+ pool = ThreadPool(max_workers)
+ else:
+ pool = Pool(max_workers)
+
+ if unpack is False:
+ results = [pool.apply_async(func, (i, ), callback=callback) for i in items]
+ else:
+ results = [pool.apply_async(func, (*i, ), callback=callback) for i in items]
+
+ if progress_bar is True:
+ bar = tqdm(total=len(results))
+ while not all([x.ready() for x in results]):
+ done_count = len([x.ready() for x in results if x.ready()])
+ bar.update(done_count - bar.n)
+ time.sleep(1)
+ bar.update(len(results) - bar.n)
+ bar.close()
+
+ pool.close()
+ pool.join()
+
+ return [r.get() for r in results]