-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrency.py
More file actions
33 lines (25 loc) · 950 Bytes
/
concurrency.py
File metadata and controls
33 lines (25 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from threading import Thread
import json
from urllib.request import urlopen
import time
import os
CITIES = ['New York', 'White plains', 'Dobbs Ferry', 'Hartsdale', 'tarrytown', 'irvington']
APIKEY = os.environ["WEATHER_APIKEY"]
class TempGetter(Thread):
def __init__(self, city):
super().__init__()
self.city = city
def run(self):
url_template = ('http://api.openweathermap.org/data/2.5/weather?q={},US&appid={}&units=imperial')
response = urlopen(url_template.format(self.city, APIKEY))
data = json.loads(response.read().decode())
self.tempreature = data['main']['temp']
threads = [TempGetter(c) for c in CITIES]
start = time.time()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
for thread in threads:
print("it is {0.tempreature:.0f}°F in {0.city}".format(thread))
print("Got {} temps in {} seconds".format(len(threads), time.time() - start))