From 1bbb7ab1de9168400b4acac0d4ee2ca7a7333f28 Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Tue, 14 Jul 2015 03:29:31 -0300 Subject: [PATCH] flags2_asyncio_executor.py closing http request\n\nSometime raises RuntimeError: Event loop is closed --- .../countries/flags2_asyncio_executor.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/17-futures/countries/flags2_asyncio_executor.py b/17-futures/countries/flags2_asyncio_executor.py index 6822c24..53ade4b 100644 --- a/17-futures/countries/flags2_asyncio_executor.py +++ b/17-futures/countries/flags2_asyncio_executor.py @@ -6,6 +6,7 @@ asyncio version using thread pool to save files import asyncio import collections +import contextlib import aiohttp from aiohttp import web @@ -28,15 +29,17 @@ class FetchError(Exception): def get_flag(base_url, cc): url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower()) resp = yield from aiohttp.request('GET', url) - if resp.status == 200: - image = yield from resp.read() - return image - elif resp.status == 404: - raise web.HTTPNotFound() - else: - raise aiohttp.HttpProcessingError( - code=resp.status, message=resp.reason, - headers=resp.headers) + with contextlib.closing(resp): + if resp.status == 200: + image = yield from resp.read() + return image + elif resp.status == 404: + raise web.HTTPNotFound() + else: + raise aiohttp.HttpProcessingError( + code=resp.status, message=resp.reason, + headers=resp.headers) + # BEGIN FLAGS2_ASYNCIO_EXECUTOR @asyncio.coroutine