-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·55 lines (45 loc) · 1.9 KB
/
main.py
File metadata and controls
executable file
·55 lines (45 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
'''
A little experiment on multiprocessing module.
The goal is to have the ability to spawn processes from multiprocessing.Process
By default that leads to AssertionError with the following text:
"Non-daemonic processes are not allowed to have children"
Requirements:
Python 3.5+ (though the method should work on python 2.7 as well)
As a modeling task, the calculation of PI with BBP algorithm was used.
'''
# STL
import multiprocessing as mp
import os, sys, re
import argparse
import logging
import decimal
# proj specific
import pi
import spawn_types
from spawn_types import check_positive
import parallel
def parse_args(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('--processes', help='number of 1st level processes to spawn',
action='store', default=2, type=check_positive)
parser.add_argument('--spawn-by', help='number of 2nd level processes to spawn',
action='store', default=2, type=check_positive)
parser.add_argument('--pi-prec', help='number of hex digits of pi that would be calculated',
action='store', type=check_positive, default=500)
parser.add_argument('--sequential', help='if set, program performs sequential calculation',
action='store_true', default=False)
return parser.parse_args(args=args)
def main():
args = parse_args()
logging.debug('args: {}'.format(args))
decimal.getcontext().prec = args.pi_prec*2 #XXX: check the dec. digits precision
if args.sequential:
print('pi(prec=%s) = %s' % (args.pi_prec, pi.calculate_pi_sequentially(args.pi_prec)))
return
p = spawn_types.NoDaemonPool(args.processes)
callback = parallel.get_worker_callback(args.spawn_by)
result = p.map(callback, parallel.get_ranges((0, args.pi_prec), args.processes))
print(sum(result))
if __name__ == '__main__':
main()