AI / Apache Burr Interview questions
Why should you use a state persister as a context manager?
Persisters hold a live database connection, and that connection needs to be closed deterministically or it leaks. For synchronous persisters, Burr currently closes the connection when the persister object is garbage collected — but that is not guaranteed to happen promptly, and it is not possible at all for asynchronous persisters, since __del__ can’t reliably run async cleanup code.
with SQLLitePersister.from_values(db_path=".sqllite.db", table_name="burr_state") as state_persister: app = ( ApplicationBuilder() .with_actions(...) .with_transitions(...) .initialize_from(state_persister, ...) .with_state_persister(state_persister) .with_identifiers(app_id=app_id) .build() ) *_, state = app.run(...)
Using with ... as state_persister: guarantees the connection is closed as soon as the block exits, even on an exception. If a context manager doesn’t fit your control flow, the equivalent is to call state_persister.cleanup() explicitly inside a finally block.
More Related questions...