On this page

Cleanup and memory ownership๏ƒ

Beginner. Cleanup without ceremony.

Excerpt. Define the connection and worker classes with their cleanup methods first. Registration declares how to dispose them; meld() creates the instances whose matched methods run when the conduit ends.

Declare your teardown vocabulary โ€” the method names that mean "clean yourself up" in this system โ€” and Melder calls them on the way down. No base class, no protocol, no context-manager ceremony:

book = md.Spellbook()

book.bind(
    spell=PooledConnection,
    existence="unique",
    disposal_method_names=["close", "shutdown"],   # this binding's ordered methods
)
book.bind(
    spell=BackgroundWorker,
    existence="unique",
    disposal_method_names=["shutdown"],
)

conduit = book.conjure()
connection = conduit.meld("PooledConnection")
worker = conduit.meld("BackgroundWorker")
conduit.cleanup()     # calls each creation's matched methods in their declared order

Each bind's list belongs to that spell, not every later bind. Melder resolves matching class methods once at bind time and preserves their order:

  • If the connection implements both names, close() runs before shutdown().

  • The worker uses its own shutdown() declaration.

  • Missing names are omitted; duplicate names execute once.

For book-wide methods, use SpellbookConfiguration. Its matching methods form one ordered block: last by default, or first with with_enforce_priority_disposal_methods(True). Shared names belong to the book's block in either mode; spell-only names retain their own order. See the configuration guide for both arrangements.

Within each store, objects are disposed newest first, so a dependency outlives the objects built on it. A failing method does not stop the others: every declared method of every object runs, each failure is chained from what the method raised, and they are raised together as one ExceptionGroup once the scope has ended. A managed_spell_quantity return normally resets the object for reuse. Its disposal methods run when the pool destroys it โ€” an elastic trim, a reset that raises, or the root's teardown.

Runnable examples๏ƒ

All beginner examples ยท Level contents ยท Full contents

Canonical page source