NCATSTranslator / Knowledge_Graph_Exchange_Registry

The Biomedical Data Translator Consortium site for development of Knowledge Graph Exchange Standards and Registry
MIT License
5 stars 3 forks source link

Prototype Refinement: the AIOHTTP session life cycle should be a singleton across HTTP requests in the application? #20

Closed RichardBruskiewich closed 3 years ago

RichardBruskiewich commented 3 years ago

We use AIOHTTP to facilate async access to files across the web. Best practices suggest that the AIOHTTP client session should be managed as a singleton (not done this way as the moment). See https://docs.aiohttp.org/en/stable/client_quickstart.html#make-a-request.

Unsure how to robustly integrate the session lifecycle globally into the Python Flask/Connexion application.

kennethbruskiewicz commented 3 years ago

Sqlite?

RichardBruskiewich commented 3 years ago

Hi @kbruskiewicz sorry about the confusion. The ClientSession purely relates to the AIOHTTP internet session (session class).

The basic form is:

    async with aiohttp.ClientSession() as session:
        async with session.get('http://httpbin.org/get') as resp:
            print(resp.status)
            print(await resp.text())

but in the above article link, it is suggested that the ClientSession object be maintained globally in the application, not local to a given request. I just don't know where to put it while ensuring that it can be cleanly closed once the application shuts down.Namely:

session = aiohttp.ClientSession()

    # Flask/connexion application running...

    # periodically,  with some URL  access
    async with session.get('...'):
         #  processing URL response here

    # ...then the application running  running  some  more...

# Application then shutting down so...
await session.close()
RichardBruskiewich commented 3 years ago

Resolved by get_global_session(cls) method in the KgeaSession class of the kgea.server.web_services.kgea_sessionmodule.