Last active
October 5, 2016 18:15
-
-
Save eduardompinto/62a60aca7eb8a9745f63535dd48def6e to your computer and use it in GitHub Desktop.
async for - Requests async com Python 3.6
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
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | |
<html><head> | |
</head><body> | |
The Project Gutenberg EBook of The Adventures of Sherlock Holmes | |
by Sir Arthur Conan Doyle | |
(#15 in our series by Sir Arthur Conan Doyle) | |
Copyright laws are changing all over the world. Be sure to check the | |
copyright laws for your country before downloading or redistributing | |
this or any other Project Gutenberg eBook. | |
This header should be the first thing seen when viewing this Project | |
Gutenberg file. Please do not remove it. Do not change or edit the | |
header without written permission. | |
Please read the "legal small print," and other information about the | |
eBook and Project Gutenberg at the bottom of this file. Included is | |
important information about your specific rights and restrictions in | |
how the file may be used. You can also find out about how to make a | |
donation to Project Gutenberg, and how to get involved. | |
**Welcome To The World of Free Plain Vanilla Electronic Texts** | |
**eBooks Readable By Both Humans and By Computers, Since 1971** | |
*****These eBooks Were Prepared By Thousands of Volunteers!***** | |
Title: The Adventures of Sherlock Holmes | |
Author: Sir Arthur Conan Doyle | |
Release Date: March, 1999 [EBook #1661] | |
[Most recently updated: November 29, 2002] | |
Edition: 12 | |
Language: English | |
Character set encoding: ASCII | |
*** START OF THE PROJECT GUTENBERG EBOOK, THE ADVENTURES OF SHERLOCK HOLMES *** | |
(Additional editing by Jose Menendez) | |
</body></html> |
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 | |
from aiohttp import ClientSession | |
async def fetch(url): | |
async with ClientSession() as session: | |
async with session.get(url) as resp: | |
return url, resp.status | |
async def fetch_urls(urls): | |
futures = [fetch(url) for url in urls] | |
yield await asyncio.gather(*futures) | |
async def run(): | |
url = 'http://localhost:8080/{}' | |
urls = [url.format(i) for i in range(100)] | |
async for result in fetch_urls(urls): | |
print(result) | |
loop = asyncio.get_event_loop() | |
try: | |
loop.run_until_complete(run()) | |
finally: | |
loop.close() |
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
aiohttp |
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
# stoled from https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html | |
import asyncio | |
from datetime import datetime | |
from aiohttp import web | |
import random | |
# set seed to ensure async and sync client get same distribution of delay values | |
# and tests are fair | |
random.seed(1) | |
async def hello(request): | |
name = request.match_info.get("name", "foo") | |
n = datetime.now().isoformat() | |
delay = random.randint(0, 3) | |
await asyncio.sleep(delay) | |
headers = {"content_type": "text/html", "delay": str(delay)} | |
# opening file is not async here, so it may block, to improve | |
# efficiency of this you can consider using asyncio Executors | |
# that will delegate file operation to separate thread or process | |
# and improve performance | |
# https://docs.python.org/3/library/asyncio-eventloop.html#executor | |
# https://pymotw.com/3/asyncio/executors.html | |
with open("big.html", "rb") as html_body: | |
print("{}: {} delay: {}".format(n, request.path, delay)) | |
response = web.Response(body=html_body.read(), headers=headers) | |
return response | |
app = web.Application() | |
app.router.add_route("GET", "/{name}", hello) | |
web.run_app(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment