title: How to reuse aiohttp ClientSession pool?
body: <p>The docs say to reuse the ClientSession:</p>
  <blockquote>
  <p>Don’t create a session per request. Most likely you need a session per
  application which performs all requests altogether.</p>
  <p>A session contains a connection pool inside, connection reusage and
  keep-alives (both are on by default) may speed up total performance.<a href="https://docs.aiohttp.org/en/stable/client_quickstart.html" rel="nofollow noreferrer">1</a></p>
  </blockquote>
  <p>But there doesn't seem to be any explanation in the docs about how to do this? There is one example that's maybe relevant, but it does not show how to reuse the pool elsewhere: <a href="http://aiohttp.readthedocs.io/en/stable/client.html#keep-alive-connection-pooling-and-cookie-sharing" rel="nofollow noreferrer">http://aiohttp.readthedocs.io/en/stable/client.html#keep-alive-connection-pooling-and-cookie-sharing</a></p>
  <p>Would something like this be the correct way to do it?</p>
  <pre><code>@app.listener('before_server_start')
  async def before_server_start(app, loop):
      app.pg_pool = await asyncpg.create_pool(**DB_CONFIG, loop=loop, max_size=100)
      app.http_session_pool = aiohttp.ClientSession()
  
  
  @app.listener('after_server_stop')
  async def after_server_stop(app, loop):
      app.http_session_pool.close()
      app.pg_pool.close()
  
  
  @app.post(&quot;/api/register&quot;)
  async def register(request):
      # json validation
      async with app.pg_pool.acquire() as pg:
          await pg.execute()  # create unactivated user in db
          async with app.http_session_pool as session:
              # TODO send activation email using SES API
              async with session.post('http://httpbin.org/post', data=b'data') as resp:
                  print(resp.status)
                  print(await resp.text())
          return HTTPResponse(status=204)
  </code></pre>