Created
February 27, 2016 11:37
-
-
Save tcuthbert/b7b9ed1c97a3a4c721e4 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 asyncio | |
import urllib.request | |
import requests | |
import time | |
import random | |
DELAY = False | |
WEBSITES = [ | |
"http://python.org/", | |
"http://python.org/", | |
"http://python.org/", | |
"http://docker.com", | |
"http://python.org/", | |
"http://docker.com", | |
"http://gobyexample.com", | |
"http://python.org/", | |
"http://docker.com", | |
"http://gobyexample.com", | |
"http://python.org/", | |
"http://docker.com", | |
"http://python.org/", | |
"http://docker.com", | |
"http://python.org/", | |
"http://docker.com", | |
"http://gobyexample.com", | |
"http://gobyexample.com", | |
"http://python.org/", | |
"http://python.org/", | |
"http://docker.com", | |
"http://gobyexample.com", | |
"http://docker.com", | |
"http://gobyexample.com", | |
"http://gobyexample.com", | |
"http://gobyexample.com", | |
"http://docker.com", | |
"http://gobyexample.com", | |
"http://docker.com", | |
"http://gobyexample.com" | |
] | |
@asyncio.coroutine | |
def get_data(future, site, idx): | |
def get_response(): | |
dat = requests.get(site) | |
future.set_result((idx, dat.text[0:25])) | |
yield from asyncio.sleep(random.randint(1, 5)) | |
get_response() | |
def print_result(future): | |
print(future.result()) | |
if __name__ == "__main__": | |
loop = asyncio.get_event_loop() | |
futures = [ asyncio.Future() for i in WEBSITES ] | |
tasks = [ asyncio.ensure_future(get_data(futures[idx], site, idx)) for idx, site in enumerate(WEBSITES) ] | |
for future in futures: | |
future.add_done_callback(print_result) | |
loop.run_until_complete(asyncio.wait(tasks)) | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment