Skip to content Skip to sidebar Skip to footer

How To Use An Aiohttp Clientsession With Sanic?

I am trying to understand what is the right way to use aiohttp with Sanic. From aiohttp documentation, I find the following: Don’t create a session per request. Most likely you

Solution 1:

In order to use a single aiohttp.ClientSession we need to instantiate the session only once and use that specific instance in the rest of the application.

To achieve this we can use a before_server_start listener which will allow us to create the instance before the app serves the first byte.

from sanic import Sanic 
from sanic.response import json

import aiohttp

app = Sanic(__name__)

@app.listener('before_server_start')definit(app, loop):
    app.aiohttp_session = aiohttp.ClientSession(loop=loop)

@app.listener('after_server_stop')deffinish(app, loop):
    loop.run_until_complete(app.aiohttp_session.close())
    loop.close()

@app.route("/")asyncdeftest(request):
    """
    Download and serve example JSON
    """
    url = "https://api.github.com/repos/channelcat/sanic"asyncwith app.aiohttp_session.get(url) as response:
        returnawait response.json()


app.run(host="0.0.0.0", port=8000, workers=2)

Breakdown of the code:

  • We are creating an aiohttp.ClientSession, passing as argument the loop that Sanic apps create at the start, avoiding this pitfall in the process.
  • We store that session in the Sanic app.
  • Finally, we are using this session to make our requests.

Solution 2:

That is essentially what I am doing.

I created a module (interactions.py) that has, for example a function like this:

asyncdefget(url, headers=None, **kwargs):
    asyncwith aiohttp.ClientSession() as session:
        log.debug(f'Fetching {url}')
        asyncwith session.get(url, headers=headers, ssl=ssl) as response:
            try:
                returnawait response.json()
            except Exception as e:
                log.error(f'Unable to complete interaction: {e}')
                returnawait response.text()

Then I just await on that:

results = await interactions.get(url)

I am not sure why that is not the "right way". The session (at least for my needs) can be closed as soon as my request is done.

Post a Comment for "How To Use An Aiohttp Clientsession With Sanic?"