added closing context manager to http response
This commit is contained in:
parent
3dbd22af09
commit
333615d051
@ -1,6 +1,6 @@
|
||||
"""Download flags of countries (with error handling).
|
||||
|
||||
asyncio version
|
||||
asyncio yield-from version
|
||||
|
||||
Sample run::
|
||||
|
||||
@ -18,6 +18,7 @@ Sample run::
|
||||
# BEGIN FLAGS2_ASYNCIO_TOP
|
||||
import asyncio
|
||||
import collections
|
||||
from contextlib import closing
|
||||
|
||||
import aiohttp
|
||||
from aiohttp import web
|
||||
@ -40,6 +41,7 @@ class FetchError(Exception): # <1>
|
||||
def get_flag(base_url, cc): # <2>
|
||||
url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
|
||||
resp = yield from aiohttp.request('GET', url)
|
||||
with closing(resp):
|
||||
if resp.status == 200:
|
||||
image = yield from resp.read()
|
||||
return image
|
||||
|
@ -2,10 +2,6 @@
|
||||
|
||||
asyncio version using thread pool to save files
|
||||
|
||||
Sample run::
|
||||
|
||||
$
|
||||
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
|
@ -1,18 +1,6 @@
|
||||
"""Download flags of countries (with error handling).
|
||||
|
||||
asyncio version
|
||||
|
||||
Sample run::
|
||||
|
||||
$ python3 flags2_asyncio.py -s ERROR -e -m 200
|
||||
ERROR site: http://localhost:8003/flags
|
||||
Searching for 676 flags: from AA to ZZ
|
||||
200 concurrent connections will be used.
|
||||
--------------------
|
||||
146 flags downloaded.
|
||||
363 not found.
|
||||
167 errors.
|
||||
Elapsed time: 2.59s
|
||||
asyncio async/await version
|
||||
|
||||
"""
|
||||
# BEGIN FLAGS2_ASYNCIO_TOP
|
||||
|
Loading…
Reference in New Issue
Block a user