Skip to content

Shared state context for actions #2

@iduuck

Description

@iduuck

It's very unhealthy for the database connection pools to always establish a new connection on each run. Also it's pretty bad to have a database connection always running (since there is maximum limit).

Hence, I would like to have a feature in the interval-py project to have a shared context for the actions, that you can access through the io object.

Imagine something like this (API borrowed from FastAPI):

@interval.on_event("startup")
async def startup_event(io: IO):
    """
    Connect to the db.
    """
    io.state['pool'] = await create_pool(...)

Which then can be used generally:

@interval.action
async def any_action(io: IO):
    async with io.state['pool'].acquire() as con:
        await con.fetch('SELECT 1')

		return f"Hello {name}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions