Skip to content

Singletons for AsyncIO #105

@Gr1N

Description

@Gr1N

Hey!

First of all, thank you for the library; I love the simplicity of the design and how it can be easily used in projects.

However, it seems impossible to have singletons for AsyncIO components. Yes, we can have them as providers and async context managers, as it stated in the examples:

@contextlib.asynccontextmanager
async def get_conn_async():
    obj = MockConnection()
    await obj.connect()
    yield obj
    await obj.destroy()

def config(binder):
    binder.bind_to_provider(MockConnection, get_conn_sync)

inject.configure(config)

@inject.autoparams()
def example(conn: MockConnection):
    # Connection and file will be automatically destroyed on exit.
    pass

It can be a good approach for simple cases, but I don't want to open database connections each time I call some function. Of course, because of the nature of AsyncIO, I think you stuck to this design, and probably without something like await inject.wait_shutdown() with manual control, we can't achieve singletons.

Nevertheless, maybe I'm missing something. Is it possible to inject a single database connection without any additional magic?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions