scientific_comp_projects/CODE/parallel_computing/functional_programming/sixthL_concurrentFunc.py
2021-10-29 15:16:40 +02:00

46 lines
1.2 KiB
Python

from functools import reduce
import concurrent.futures
import collections
import os
from pprint import pprint
import itertools
Scientist = collections.namedtuple('Scientist', [
'name',
'field',
'born',
'nobel',
])
scientists = (
Scientist(name='Ada Lovelace', field='math', born=1815, nobel=False),
Scientist(name='Emmy Noether', field='math', born=1882, nobel=False),
Scientist(name='Marie Curie', field='physics', born=1867, nobel=True),
Scientist(name='Tu Youyou', field='chemistry', born=1930, nobel=True),
Scientist(name='Ada Yonath', field='chemistry', born=1939, nobel=True),
Scientist(name='Vera Rubin', field='astronomy', born=1928, nobel=False),
Scientist(name='Sally Ride', field='physics', born=1951, nobel=True),
)
import time
def transform(x):
print(f'Processing {os.getpid()} work record {x.name}')
time.sleep(1)
result = {'name': x.name, 'age': 2021 - x.born}
print(f'Done processing {os.getpid()} record {x.name}')
return result
start = time.time()
with concurrent.futures.ProcessPoolExecutor() as executor:
result = executor.map(transform, scientists)
end = time.time()
print(f'Time to complete: {end-start} \n')
pprint(tuple(result))