Since this thing is on the innerwebs I suppose it should have a
[python_utils.git] / tests / parallelize_itest.py
index 9d9871053aaa927c1af5508a17db1522b0bbd65a..ef154a70d92eb4a32339bc4eb5a121561878aa50 100755 (executable)
@@ -1,15 +1,20 @@
 #!/usr/bin/env python3
 
-import random
+# © Copyright 2021-2022, Scott Gasch
+
+"""parallelize unittest."""
+
+import logging
 import sys
 
 import bootstrap
-import parallelize as p
 import decorator_utils
 import executors
-import math_utils
+import parallelize as p
 import smart_future
 
+logger = logging.getLogger(__name__)
+
 
 @p.parallelize(method=p.Method.THREAD)
 def compute_factorial_thread(n):
@@ -28,19 +33,19 @@ def compute_factorial_process(n):
 
 
 @p.parallelize(method=p.Method.REMOTE)
-def list_primes(n):
-    """Calculates sum of all primes below given integer n"""
-    ret = []
+def compute_factorial_remote(n):
+    total = 1
     for x in range(2, n):
-        ret.append(math_utils.is_prime(x))
-    return ret
+        total *= x
+    return total
 
 
 @decorator_utils.timed
 def test_thread_parallelization() -> None:
     results = []
     for _ in range(50):
-        results.append(compute_factorial_thread(_))
+        f = compute_factorial_thread(_)
+        results.append(f)
     smart_future.wait_all(results)
     for future in results:
         print(f'Thread: {future}')
@@ -61,14 +66,11 @@ def test_process_parallelization() -> None:
 
 @decorator_utils.timed
 def test_remote_parallelization() -> None:
-    results = {}
-    for _ in range(50):
-        n = random.randint(0, 100000)
-        results[n] = list_primes(n)
-    tot = 0
-    for _ in results[n]:
-        tot += _
-    print(tot)
+    results = []
+    for _ in range(10):
+        results.append(compute_factorial_remote(_))
+    for result in smart_future.wait_any(results):
+        print(result)
     rexecutor = executors.DefaultExecutors().remote_pool()
     rexecutor.shutdown()
 
@@ -82,4 +84,8 @@ def main() -> None:
 
 
 if __name__ == '__main__':
-    main()
+    try:
+        main()
+    except Exception as e:
+        logger.exception(e)
+        sys.exit(1)