Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import collections
  2. import multiprocessing
  3. import os
  4. import time
  5. from pprint import pprint
  6.  
  7. Student = collections.namedtuple('Student', [
  8. 'name',
  9. 'born',
  10. 'gender',
  11. 'location',
  12. ])
  13.  
  14. students = (
  15. Student(name='Valentyn Marusyk', born=1998, gender='male', location='Chernivtsi'),
  16. Student(name='Debora Jheryada', born=1999, gender='female', location='Chernivtsi'),
  17. Student(name='Vitaliy Kapaniyk', born=1998, gender='male', location='Ivano-Frankivsk'),
  18. Student(name='Sanya Tatarchyk', born=1996, gender='male', location='Chernivtsi'),
  19. Student(name='Sanya Pankevich', born=1998, gender='male', location='Chernivtsi'),
  20. Student(name='Denis Malaiko', born=1998, gender='male', location='Chernivtsi'),
  21. )
  22. pprint(students)
  23. print()
  24.  
  25.  
  26. def transform(x):
  27. print(f'Process {os.getpid()} working record {x.name}')
  28. time.sleep(1)
  29. result = {'name': x.name, 'age': 2019 - x.born}
  30. print(f'Process {os.getpid()} done processing record {x.name}')
  31. return result
  32.  
  33.  
  34. start = time.time()
  35.  
  36. pool = multiprocessing.Pool()
  37. result = pool.map(transform, students)
  38. #result = tuple(map(transform, students))
  39.  
  40. end = time.time()
  41.  
  42. print(f'\n Time to complete: {end - start:.2f}s\n')
  43. pprint(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement