def print_weather(date): request_start = time.time() response = requests.get(f'http://weather.com/london/{date}') request_time = time.time() - request_start print(f'{date}: {response} (request took {request_time:.1f}s)')
def get_quote(): return requests.get('http://shakespeare.com/quote')
def get_temperature(city, date): """Fetches the temperature in the given city, on the given date""" url = f'http://world_temperature.com/{city}/{date:%Y%m%d}' response = requests.get(url, max_wait=0.01) return float(response)
def run(self): self.response = requests.get(f'http://weather.com/london/{self.date}')
prints out all results at the end. Update the code so that it uses: * A concurrent.futures thread pool to make the web requests * The submit method, to return futures for each request * The concurrent.futures.as_completed function, to print results out in the order that they complete, as they complete. Note: You might want to create a new function (e.g. `perform_check`), which will perform a web request, and then return the url that was called, rather than the response. This will make it easier to work out which url corresponds to each future. """ import time import fake_requests as requests # A list of 10 website endpoints which will return at different speeds TEST_URLS = [ f"http://www.speed-testing-website.com/test-{i}" for i in range(1, 11) ] speeds = {} for url in TEST_URLS: start = time.time() requests.get(url) speeds[url] = time.time() - start for url, duration in sorted(speeds.items(), key=lambda x: x[1]): print(f"{url} (took {duration:.2f}s)")
def get_weather(date): return requests.get(f'http://weather.com/london/{date}')