Skip to content

Instantly share code, notes, and snippets.

@Day0Dreamer
Created December 24, 2017 16:58
Show Gist options
  • Save Day0Dreamer/4fe8939b95c214c7c2178c2c838f34e3 to your computer and use it in GitHub Desktop.
Save Day0Dreamer/4fe8939b95c214c7c2178c2c838f34e3 to your computer and use it in GitHub Desktop.
"""
Python Parallel Processing (in 60 seconds or less)
https://dbader.org/blog/python-parallel-computing-in-60-seconds
"""
import collections
import multiprocessing
Scientist = collections.namedtuple('Scientist', [
'name',
'born',
])
scientists = (
Scientist(name='Ada Lovelace', born=1815),
Scientist(name='Emmy Noether', born=1882),
Scientist(name='Marie Curie', born=1867),
Scientist(name='Tu Youyou', born=1930),
Scientist(name='Ada Yonath', born=1939),
Scientist(name='Vera Rubin', born=1928),
Scientist(name='Sally Ride', born=1951),
)
def process_item(item):
return {
'name': item.name,
'age': 2017 - item.born
}
if __name__ == '__main__':
pool = multiprocessing.Pool()
result = pool.map(process_item, scientists)
print(tuple(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment