From 165245fa270c8d205330d57064851650af91e685 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sat, 6 Aug 2016 00:57:35 +0300 Subject: [PATCH] Encourage async with ClientSession() in docs --- README.rst | 10 ++++++---- docs/client_reference.rst | 11 ++++++++--- docs/index.rst | 17 +++++++++-------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index f400eee36e8..a7a8600fffe 100644 --- a/README.rst +++ b/README.rst @@ -43,12 +43,14 @@ To retrieve something from the web: async with session.get(url) as response: return await response.text() + async def main(loop): + async with aiohttp.ClientSession(loop=loop) as session: + html = await fetch(session, 'http://python.org') + print(html) + if __name__ == '__main__': loop = asyncio.get_event_loop() - with aiohttp.ClientSession(loop=loop) as session: - html = loop.run_until_complete( - fetch(session, 'http://python.org')) - print(html) + loop.run_until_complete(main(loop)) Server diff --git a/docs/client_reference.rst b/docs/client_reference.rst index 0d9a26db939..56c78f9e38c 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -29,9 +29,14 @@ Usage example:: assert resp.status == 200 return await resp.text() - with aiohttp.ClientSession() as client: - html = asyncio.get_event_loop().run_until_complete(fetch(client)) - print(html) + async def main(loop): + async with aiohttp.ClientSession(loop=loop) as client: + html = await fetch(client) + print(html) + + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) + .. versionadded:: 0.17 diff --git a/docs/index.rst b/docs/index.rst index a4f3f6ad6ac..0d4d30f76f7 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -40,20 +40,21 @@ Getting Started Client example:: - import asyncio import aiohttp + import asyncio - async def fetch_page(session, url): + async def fetch(session, url): with aiohttp.Timeout(10): async with session.get(url) as response: - assert response.status == 200 - return await response.read() + return await response.text() + + async def main(loop): + async with aiohttp.ClientSession(loop=loop) as session: + html = await fetch(session, 'http://python.org') + print(html) loop = asyncio.get_event_loop() - with aiohttp.ClientSession(loop=loop) as session: - content = loop.run_until_complete( - fetch_page(session, 'http://python.org')) - print(content) + loop.run_until_complete(main(loop)) Server example::