def close(prefix_name: Optional[str])
Gracefully shut down dbzero, persisting changes and releasing resources.
When called without arguments, it closes all open prefixes and terminates the entire dbzero instance.
After full close, dbzero methods will raise exceptions until re-initialized.
To use dbzero again, you'll need to re-initialize it with dbzero.init().
Alternatively, you can provide a specific prefix name as an argument to close only that prefix while keeping others open.
Garbage Collection: Objects that are no longer referenced (e.g., by another object or tags) are permanently deleted.
Parameters
prefix_namestr, optional
Optional name of the prefix to close. If omitted, all prefixes and the dbzero instance are closed.
Returns
This method does not return any value.
Examples
Closing and re-initializind dbzero
This example shows the standard pattern of committing changes and then closing the dbzero completely.
import dbzero as db0
import shutil
DB0_DIR = "/tmp/my-app-db"
# Setup and create an object
db0.init(DB0_DIR)
db0.open("my-prefix")
obj = MemoTestClass(value=123)
# Assigning a tag keeps the object from being garbage collected after close()
db0.tags(obj).add("KEEP")
# The python instance becomes invalid after close()
# Keep the object UUID to access it again later
obj_uuid = db0.uuid(obj)
# Commit changes and close dbzero
db0.close()
# Re-initialize and re-open to verify data
# This would fail if dbzero wasn't closed and re-initialized
db0.init(DB0_DIR)
db0.open("my-prefix", "r")
obj = db0.fetch(obj_uuid)
assert obj.value == 123
# The object will be deleted on close() after removing tag - no more references in dbzero
db0.tags(obj).remove("KEEP")
# Final cleanup
db0.close()
shutil.rmtree(DB0_DIR)Closing specific prefix
You can also close a single prefix while leaving others operational. This is useful for managing different parts of your application's state independently.
import dbzero as db0
import shutil
DB0_DIR = "/tmp/my-app-db"
db0.init(DB0_DIR)
# Open two prefixes
db0.open("users")
db0.open("products")
# ...
# Close the 'users' prefix but keep 'products' open
# Prefix 'users' can now be accessed for RW from another process
db0.close("users")
# You can still work with the 'products' prefix
current_prefix = db0.get_current_prefix().name
assert current_prefix == "products"
obj = SomeProduct(quantity=123)
# ...
# Final cleanup
db0.close()
shutil.rmtree(DB0_DIR)