Created
August 17, 2019 12:03
-
-
Save norbinsh/0bcc2beb0579694ac3af4e92fa6c27b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
import time | |
import datetime | |
import collections | |
import os | |
import requests | |
from pprint import pprint | |
def timer(func): | |
def function_wrapper(x): | |
start = datetime.datetime.now() | |
func(x) | |
total = datetime.datetime.now() - start | |
return total | |
return function_wrapper | |
Scientist = collections.namedtuple('Scientist', 'name, field, born, nobel') | |
scientists = ( | |
Scientist(name='Shay', field='math', born='1987', nobel=False), | |
Scientist(name='Nurit', field='math', born='1988', nobel=False), | |
Scientist(name='Igor', field='physics', born='1970', nobel=True), | |
Scientist(name='Abdul', field='robotics', born='1960', nobel=False), | |
Scientist(name='Shay', field='math', born='1987', nobel=False), | |
Scientist(name='Nurit', field='math', born='1988', nobel=False), | |
Scientist(name='Igor', field='physics', born='1970', nobel=True), | |
Scientist(name='Abdul', field='robotics', born='1960', nobel=False), | |
) | |
def read_google(): | |
r = requests.get('https://www.google.com/') | |
print(r.text) | |
return r.status_code | |
def transform(x): | |
print(f"Process {os.getpid()} is currently processing record: {x.name}") | |
read_google() | |
result = {'name': x.name, 'age': 2017 - int(x.born)} | |
print(f"finished processing record: {x.name}") | |
return result | |
pool = multiprocessing.Pool() | |
@timer | |
def map_it(your_iterator): | |
result = pool.map(transform, your_iterator) | |
return result | |
pprint(map_it(scientists)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment