Is there any way of creating a service using functions? #5300
Replies: 1 comment
|
Short answer: no, BentoML 1.x has no function-based service definition. @bentoml.service only accepts a class. The decorator signature is def service(inner: type[T], ...) and the docstring says "Mark a class as a BentoML service" (src/_bentoml_sdk/service/factory.py). The documented pattern is decorating a class, and there is no functional overload. That doesn't force your service to hold state though. A class is just the registration vehicle. Keep it stateless by storing nothing mutable on self, initializing clients or resources in init only if they are read-only, and putting the logic in plain module-level functions that the @api methods call. Something like a module-level summarize_text(text) function plus a thin class whose @api method just returns summarize_text(text). If you truly want no class at all, plain functions can be exposed by mounting an ASGI app (for example FastAPI) with bentoml.mount_asgi_app. That works, but then you leave the @bentoml.service machinery behind, including adaptive batching, so it isn't equivalent, just a different way to serve functions. |
Uh oh!
There was an error while loading. Please reload this page.
I had bad experience with class based controllers in the past, since classes allow state in route definitions and I prefer my apis to be stateless. I'm reading docs and I see that I can create services like this:
Is there any way to turn this into a function in order to force the service to be stateless? like this:
All reactions