forked from hackers-test/hack-5-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.py
More file actions
26 lines (21 loc) · 1019 Bytes
/
Copy pathparallel.py
File metadata and controls
26 lines (21 loc) · 1019 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
25
26
import numpy as np
import sys
import time
from concurrent.futures import ProcessPoolExecutor, as_completed
## Use sys.argv to pass in an integer value for the number
## of consecutive integers to operate on
length = int(sys.argv[1])
def task(n):
time.sleep(1)
return n * n #square of number passed in
if __name__ == '__main__':
numbers = range(0, length) #takes integer value passed in
## create a pool of workers
## max_workers determines the number of parallel process to run
with ProcessPoolExecutor(max_workers=4) as executor: #this is number of parallel processeors!!!
## Submit all the jobs to the executor
results = [executor.submit(task, num) for num in numbers] #send tasks to CPU worker pool
## As jobs finish, pull the results and print them
for future in as_completed(results): #taking list and detecting when job is finished
#detects completion when worker pools finish their 'tasks'
print(f"Result: {future.result()}")