Skip to content

Commit

Permalink
Encourage async with ClientSession() in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Aug 5, 2016
1 parent 2a8964f commit 165245f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
10 changes: 6 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 9 additions & 8 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down

0 comments on commit 165245f

Please sign in to comment.