Created
June 22, 2016 17:19
-
-
Save cool-RR/5b8f8a03bb036e4e58e3a1e990d14830 to your computer and use it in GitHub Desktop.
Python concurrency
This file contains hidden or 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
| #!python | |
| import math | |
| import sys | |
| import concurrent.futures | |
| import requests | |
| def get_age(name): | |
| processed_name = name.replace(' ', '_') | |
| response = requests.get( | |
| 'https://en.wikipedia.org/wiki/%s' % processed_name | |
| ) | |
| age_prefix = '(age ' | |
| if age_prefix in response.text: | |
| location = response.text.find(age_prefix) | |
| return response.text[location + len(age_prefix) : | |
| location + len(age_prefix) + 2] | |
| people = [ | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| ] | |
| if __name__ == '__main__': | |
| with concurrent.futures.ProcessPoolExecutor(50) as executor: | |
| ages = executor.map(get_age, people) | |
| for person, age in zip(people, ages): | |
| print('%s is %s years old' % (person, age)) | |
| #for person in people: | |
This file contains hidden or 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
| #!python | |
| import math | |
| import sys | |
| import concurrent.futures | |
| import requests | |
| def get_age(name): | |
| processed_name = name.replace(' ', '_') | |
| response = requests.get( | |
| 'https://en.wikipedia.org/wiki/%s' % processed_name | |
| ) | |
| age_prefix = '(age ' | |
| if age_prefix in response.text: | |
| location = response.text.find(age_prefix) | |
| return response.text[location + len(age_prefix) : | |
| location + len(age_prefix) + 2] | |
| people = [ | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| ] | |
| with concurrent.futures.ThreadPoolExecutor(100) as executor: | |
| ages = executor.map(get_age, people) | |
| for person, age in zip(people, ages): | |
| print('%s is %s years old' % (person, age)) | |
| #for person in people: | |
This file contains hidden or 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
| #!python | |
| import math | |
| import sys | |
| import concurrent.futures | |
| import threading | |
| import multiprocessing | |
| import queue as queue_module | |
| import requests | |
| from python_toolbox import nifty_collections | |
| class AgeException(Exception): | |
| pass | |
| def get_age(name): | |
| processed_name = name.replace(' ', '_') | |
| response = requests.get( | |
| 'https://en.wikipedia.org/wiki/%s' % processed_name | |
| ) | |
| age_prefix = '(age ' | |
| if age_prefix in response.text: | |
| location = response.text.find(age_prefix) | |
| return response.text[location + len(age_prefix) : | |
| location + len(age_prefix) + 2] | |
| else: | |
| pass #raise AgeException | |
| people = [ | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| 'Bill Gates', | |
| 'Chuck Norris', | |
| 'Prince', | |
| 'Michael Jackson', | |
| 'Justin Bieber', | |
| 'Taylor Swift', | |
| 'Dave Grohl', | |
| 'Lady Gaga', | |
| ] | |
| queue = queue_module.Queue() | |
| class AgeThread(threading.Thread): | |
| def __init__(self, person): | |
| threading.Thread.__init__(self) | |
| self.person = person | |
| self.age = None | |
| def run(self): | |
| self.age = get_age(self.person) | |
| queue.put((self.person, self.age)) | |
| if __name__ == '__main__': | |
| age_threads = [AgeThread(person) for person in people] | |
| for age_thread in age_threads: | |
| age_thread.start() | |
| while True: | |
| person, age = queue.get() | |
| queue.get() | |
| print('%s is %s years old.' % (person, age)) | |
| # | |
| # queue = queue_module.Queue() | |
| # queue.put(7) | |
| # queue.get() | |
| # multiprocessing.Queue | |
| # lock = threading.Lock() | |
| # lock2 = multiprocessing.Lock() | |
| # import sys | |
| # threading.RLock() | |
| # threading.Condition | |
| # threading.Semaphore | |
| # threading.BoundedSemaphore | |
| # threading.Barrier | |
| # | |
| # | |
| # with lock: | |
| # pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment