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).
|
"""Download flags of countries (with error handling).
|
||||||
|
|
||||||
asyncio version
|
asyncio yield-from version
|
||||||
|
|
||||||
Sample run::
|
Sample run::
|
||||||
|
|
||||||
@ -18,6 +18,7 @@ Sample run::
|
|||||||
# BEGIN FLAGS2_ASYNCIO_TOP
|
# BEGIN FLAGS2_ASYNCIO_TOP
|
||||||
import asyncio
|
import asyncio
|
||||||
import collections
|
import collections
|
||||||
|
from contextlib import closing
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
@ -40,6 +41,7 @@ class FetchError(Exception): # <1>
|
|||||||
def get_flag(base_url, cc): # <2>
|
def get_flag(base_url, cc): # <2>
|
||||||
url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
|
url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
|
||||||
resp = yield from aiohttp.request('GET', url)
|
resp = yield from aiohttp.request('GET', url)
|
||||||
|
with closing(resp):
|
||||||
if resp.status == 200:
|
if resp.status == 200:
|
||||||
image = yield from resp.read()
|
image = yield from resp.read()
|
||||||
return image
|
return image
|
||||||
|
@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
asyncio version using thread pool to save files
|
asyncio version using thread pool to save files
|
||||||
|
|
||||||
Sample run::
|
|
||||||
|
|
||||||
$
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -1,18 +1,6 @@
|
|||||||
"""Download flags of countries (with error handling).
|
"""Download flags of countries (with error handling).
|
||||||
|
|
||||||
asyncio version
|
asyncio async/await 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
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# BEGIN FLAGS2_ASYNCIO_TOP
|
# BEGIN FLAGS2_ASYNCIO_TOP
|
||||||
|
Loading…
x
Reference in New Issue
Block a user