Move parallelize_test to tests dir.
[python_utils.git] / tests / parallelize_test.py
1 #!/usr/bin/env python3
2
3 import random
4 import sys
5
6 import bootstrap
7 import parallelize as p
8 import decorator_utils
9 import executors
10 import math_utils
11
12
13 @p.parallelize(method=p.Method.REMOTE)
14 def list_primes(n):
15     """Calculates sum of all primes below given integer n"""
16     ret = []
17     for x in range(2, n):
18         ret.append(math_utils.is_prime(x))
19     return ret
20
21
22 @decorator_utils.timed
23 def driver() -> None:
24     results = {}
25     for _ in range(200):
26         n = random.randint(0, 100000)
27         results[n] = list_primes(n)
28     tot = 0
29     for _ in results[n]:
30         tot += _
31     print(tot)
32
33
34 @bootstrap.initialize
35 def main() -> None:
36     print(driver())
37     pexecutor = executors.DefaultExecutors().process_pool()
38     pexecutor.shutdown()
39     texecutor = executors.DefaultExecutors().thread_pool()
40     texecutor.shutdown()
41     rexecutor = executors.DefaultExecutors().remote_pool()
42     rexecutor.shutdown()
43     sys.exit(0)
44
45
46 if __name__ == '__main__':
47     main()
48
49 # print """Usage: python sum_primes.py [ncpus]
50 #     [ncpus] - the number of workers to run in parallel,
51 #     if omitted it will be set to the number of processors in the system
52 # """
53
54 # # tuple of all parallel python servers to connect with
55 # ppservers = ()
56 # #ppservers = ("10.0.0.1",)
57
58 # if len(sys.argv) > 1:
59 #     ncpus = int(sys.argv[1])
60 #     # Creates jobserver with ncpus workers
61 # job_server = pp.Server(ncpus, ppservers=ppservers)
62 # else:
63 #     # Creates jobserver with automatically detected number of workers
64 # job_server = pp.Server(ppservers=ppservers)
65
66 # print "Starting pp with", job_server.get_ncpus(), "workers"
67
68 # # Submit a job of calulating sum_primes(100) for execution.
69 # # sum_primes - the function
70 # # (100,) - tuple with arguments for sum_primes
71 # # (isprime,) - tuple with functions on which function sum_primes depends
72 # # ("math",) - tuple with module names which must be imported before sum_primes execution
73 # # Execution starts as soon as one of the workers will become available
74 # job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))
75
76 # # Retrieves the result calculated by job1
77 # # The value of job1() is the same as sum_primes(100)
78 # # If the job has not been finished yet, execution will wait here until result is available
79 # result = job1()
80
81 # print "Sum of primes below 100 is", result
82
83 # start_time = time.time()
84
85 # # The following submits 8 jobs and then retrieves the results
86 # inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
87 # jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]
88 # for input, job in jobs:
89 #     print "Sum of primes below", input, "is", job()
90
91 # print "Time elapsed: ", time.time() - start_time, "s"
92 # job_server.print_stats()
93
94 # # Parallel Python Software: http://www.parallelpython.com