GeezerVison is showing github.com — added closing context manager to http response · sdqscript/fluent_python_code@333615d
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: 17-futures/countries/flags2_asyncio.py +12-10Lines changed: 12 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | """Download flags of countries (with error handling). |
2 | 2 |
|
3 | | -asyncio version |
| 3 | +asyncio yield-from version |
4 | 4 |
|
5 | 5 | Sample run:: |
6 | 6 |
|
|
18 | 18 | # BEGIN FLAGS2_ASYNCIO_TOP |
19 | 19 | import asyncio |
20 | 20 | import collections |
| 21 | +from contextlib import closing |
21 | 22 |
|
22 | 23 | import aiohttp |
23 | 24 | from aiohttp import web |
@@ -40,15 +41,16 @@ def __init__(self, country_code): |
40 | 41 | def get_flag(base_url, cc): # <2> |
41 | 42 | url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower()) |
42 | 43 | resp = yield from aiohttp.request('GET', url) |
43 | | - if resp.status == 200: |
44 | | - image = yield from resp.read() |
45 | | - return image |
46 | | - elif resp.status == 404: |
47 | | - raise web.HTTPNotFound() |
48 | | - else: |
49 | | - raise aiohttp.HttpProcessingError( |
50 | | - code=resp.status, message=resp.reason, |
51 | | - headers=resp.headers) |
| 44 | + with closing(resp): |
| 45 | + if resp.status == 200: |
| 46 | + image = yield from resp.read() |
| 47 | + return image |
| 48 | + elif resp.status == 404: |
| 49 | + raise web.HTTPNotFound() |
| 50 | + else: |
| 51 | + raise aiohttp.HttpProcessingError( |
| 52 | + code=resp.status, message=resp.reason, |
| 53 | + headers=resp.headers) |
52 | 54 |
|
53 | 55 |
|
54 | 56 | @asyncio.coroutine |
|
Collapse file
Copy file name to clipboardExpand all lines: 17-futures/countries/flags2_asyncio_executor.py -4Lines changed: 0 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
2 | 2 |
|
3 | 3 | asyncio version using thread pool to save files |
4 | 4 |
|
5 | | -Sample run:: |
6 | | -
|
7 | | - $ |
8 | | -
|
9 | 5 | """ |
10 | 6 |
|
11 | 7 | import asyncio |
|
Collapse file
Copy file name to clipboardExpand all lines: 17-futures/countries/flags2_await.py +1-13Lines changed: 1 addition & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | """Download flags of countries (with error handling). |
2 | 2 |
|
3 | | -asyncio version |
4 | | -
|
5 | | -Sample run:: |
6 | | -
|
7 | | - $ python3 flags2_asyncio.py -s ERROR -e -m 200 |
8 | | - ERROR site: http://localhost:8003/flags |
9 | | - Searching for 676 flags: from AA to ZZ |
10 | | - 200 concurrent connections will be used. |
11 | | - -------------------- |
12 | | - 146 flags downloaded. |
13 | | - 363 not found. |
14 | | - 167 errors. |
15 | | - Elapsed time: 2.59s |
| 3 | +asyncio async/await version |
16 | 4 |
|
17 | 5 | """ |
18 | 6 | # BEGIN FLAGS2_ASYNCIO_TOP |
|