Skip to content

Async state persister doesn't work as a context manager #546

@kajocina

Description

@kajocina

It seems that the AsyncSQLitePersister doesn't work in the expected way when trying to use it as a context manager.

Current behavior

Trying to use AsyncSQLitePersister.from_values() with "async with" fails. Full example below.

Stack Traces

(...)
    async with AsyncSQLitePersister.from_values(
TypeError: 'coroutine' object does not support the asynchronous context manager protocol

Library & System Information

Python 3.11, MacOS

Expected behavior

Expected the provided snippet to work as it follows how the context manager is shown in the tutorials during the application build step. The tutorial shows a regular sync versions, perhaps async ones need to be initialised differently and the tutorial updated?

Steps to replicate behavior

import datetime
import asyncio

from burr.core import action, State, ApplicationBuilder, expr
from burr.integrations.persisters.b_aiosqlite import AsyncSQLitePersister


@action(reads=["counter"], writes=["counter"])
async def increment(state: State) -> State:
    """Increment the counter by 1"""

    # get the value from the `state`
    current_count = state["counter"]
    current_count += 1

    print("Count: ", current_count)

    # use `.update()` to create a new `State`
    return state.update(counter=current_count)


@action(reads=["counter"], writes=[])
async def exit_counter(state: State) -> State:
    """Print the current count and the current time"""
    current_count = state["counter"]

    print(f"Finished counting to {current_count} at {datetime.datetime.now():%H:%M:%S %Y-%m-%d}")

    return state


async def build_app():
    """This is where the problem shows up."""
    async with AsyncSQLitePersister.from_values(
            db_path=".burr.db",
            table_name="burr_state"
    ) as state_persister:
        app = await (
            ApplicationBuilder()
            .with_actions(increment, exit_counter)
            .with_transitions(
                ("increment", "increment", expr("counter < 10")),
                ("increment", "exit_counter"),
            )
            .with_state(counter=0)
            .with_entrypoint("increment")
            .with_state_persister(state_persister)
            .abuild()
        )
    return app


async def main():
    app = await build_app()
    action_obj, result, state = await app.arun(halt_after=["exit_counter"])

if __name__ == "__main__":
    asyncio.run(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions