Container lifecycle hooks
Since Modal will reuse the same container for multiple inputs, sometimes you might want to run some code exactly once when the container starts or exits.
To accomplish this, you need to use Modal’s class syntax and the @app.cls decorator. Specifically, you’ll
need to:
- Convert your function to a method by making it a member of a class.
- Decorate the class with
@app.cls(...)with same arguments you previously had for@app.function(...). - Instead of the
@app.functiondecorator on the original method, use@modal.methodor the appropriate decorator for a web endpoint. - Add the correct method “hooks” to your class based on your need:
@modal.enterfor one-time initialization (remote)@modal.exitfor one-time cleanup (remote)
@modal.enter
The container entry handler is called when a new container is started. This is useful for doing one-time initialization, such as loading model weights or importing packages that are only present in that image.
To use, make your function a member of a class, and apply the @modal.enter() decorator to one or more class methods:
When working with an asynchronous Modal app, you may use an async method instead:
Note: The @modal.enter() decorator replaces the earlier __enter__ syntax, which
has been deprecated.
@modal.exit
The container exit handler is called when a container is about to exit. It is
useful for doing one-time cleanup, such as closing a database connection or
saving intermediate results. To use, make your function a member of a class, and
apply the @modal.exit() decorator:
Exit handlers are also called when a container is preempted. The exit handler is given a grace period of 30 seconds to finish, and it will be killed if it takes longer than that to complete.
Lifecycle hooks for web endpoints
Modal @functions that are web endpoints can be
converted to the class syntax as well. Instead of @modal.method, simply use
whichever of the web endpoint decorators (@modal.fastapi_endpoint, @modal.asgi_app or @modal.wsgi_app) you were using before.