- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson07_fib_parallel.py
More file actions
Latest commit
24 lines (22 loc) · 681 Bytes
/
Copy pathlesson07_fib_parallel.py
File metadata and controls
24 lines (22 loc) · 681 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# SuperFastPython.com
# calculate some large fibonacci numbers in parallel
fromconcurrent.futuresimportProcessPoolExecutor
# calculate the nth fibonacci number
deffibonacci(n):
# start the sequence
f0, f1=0, 1
# compute the nth number
for_inrange(0, n):
f0, f1=f1, (f1+f0)
returnf0
# protect the entry point
if__name__=='__main__':
# create the process pool
withProcessPoolExecutor() asexe:
# fibonacci numbers to calculate
numbers=range(10000)
# calculate concurrently
fibs=exe.map(fibonacci, numbers)
# store results
results=dict(zip(numbers, fibs))
print('Done')