dependencies icon indicating copy to clipboard operation
dependencies copied to clipboard

Asynchronous setup and teardown for @value decorator.

Open proofit404 opened this issue 5 years ago • 2 comments

Value decorator should handle async generators.

As it turns out contextlib.AsyncExitStack can do all the cleanup job for us. All we need to do is register all context managers in it.

class Container(Injector):
    app = App

    @value
    async def db_connection(pool):
        connection = await pool.aquire()
        yield connection
        await connection.release()

async with Container as initialized:
    await initialized.app.process()

In the case of asyncio explicit loop should be added to the injection scope and required by value decorated function.

See #114

proofit404 avatar Jan 20 '21 12:01 proofit404

Hey @proofit404, I was exploring the idea of using the value decorator to handle async generators, but it appears that this feature hasn't been implemented yet. Do you have any estimates on when this might be available for us to use? I'm really excited about the potential benefits of this feature! Thanks!

balazser avatar Apr 12 '23 15:04 balazser

Hello @balazser

At the moment we have a newborn so I can't do estimations on my open source work.

But when I would come back to the dependencies project, this feature would be one of the highest priorities.

I think my answer does not help you much :sweat_smile:

Have a good day :cocktail: :palm_tree:

Regards, Josiah.

proofit404 avatar Apr 13 '23 06:04 proofit404