7 import parallelize as p
14 @p.parallelize(method=p.Method.THREAD)
15 def compute_factorial_thread(n):
22 @p.parallelize(method=p.Method.PROCESS)
23 def compute_factorial_process(n):
30 @p.parallelize(method=p.Method.REMOTE)
32 """Calculates sum of all primes below given integer n"""
35 ret.append(math_utils.is_prime(x))
39 @decorator_utils.timed
43 results.append(compute_factorial_process(_))
44 for future in smart_future.wait_any(results):
45 print(f'Process: {future}')
49 results.append(compute_factorial_thread(_))
50 smart_future.wait_all(results)
51 for future in results:
52 print(f'Thread: {future}')
56 n = random.randint(0, 100000)
57 results[n] = list_primes(n)
67 pexecutor = executors.DefaultExecutors().process_pool()
69 texecutor = executors.DefaultExecutors().thread_pool()
71 rexecutor = executors.DefaultExecutors().remote_pool()
76 if __name__ == '__main__':
79 # print """Usage: python sum_primes.py [ncpus]
80 # [ncpus] - the number of workers to run in parallel,
81 # if omitted it will be set to the number of processors in the system
84 # # tuple of all parallel python servers to connect with
86 # #ppservers = ("10.0.0.1",)
88 # if len(sys.argv) > 1:
89 # ncpus = int(sys.argv[1])
90 # # Creates jobserver with ncpus workers
91 # job_server = pp.Server(ncpus, ppservers=ppservers)
93 # # Creates jobserver with automatically detected number of workers
94 # job_server = pp.Server(ppservers=ppservers)
96 # print "Starting pp with", job_server.get_ncpus(), "workers"
98 # # Submit a job of calulating sum_primes(100) for execution.
99 # # sum_primes - the function
100 # # (100,) - tuple with arguments for sum_primes
101 # # (isprime,) - tuple with functions on which function sum_primes depends
102 # # ("math",) - tuple with module names which must be imported before sum_primes execution
103 # # Execution starts as soon as one of the workers will become available
104 # job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))
106 # # Retrieves the result calculated by job1
107 # # The value of job1() is the same as sum_primes(100)
108 # # If the job has not been finished yet, execution will wait here until result is available
111 # print "Sum of primes below 100 is", result
113 # start_time = time.time()
115 # # The following submits 8 jobs and then retrieves the results
116 # inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
117 # jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]
118 # for input, job in jobs:
119 # print "Sum of primes below", input, "is", job()
121 # print "Time elapsed: ", time.time() - start_time, "s"
122 # job_server.print_stats()
124 # # Parallel Python Software: http://www.parallelpython.com