bentoml / BentoML

The easiest way to serve AI apps and models - Build Model Inference APIs, Job queues, LLM apps, Multi-model pipelines, and more!
https://bentoml.com
Apache License 2.0
7.16k stars 792 forks source link

bug: how can i run scheduling tasks using bentoml #3173

Open math-sasso opened 2 years ago

math-sasso commented 2 years ago

Describe the bug

In the code below I schedule a task to run every 3 seconds.

import time
from datetime import datetime
import bentoml
from apscheduler.schedulers.background import BackgroundScheduler
from fastapi import FastAPI, APIRouter

sched = BackgroundScheduler()
app = FastAPI()
service = bentoml.Service("bentoml-service")

class Router:
    def __init__(self):
        self.router = APIRouter()
        self.router.add_api_route("tick", self.tick, methods=["POST"])
        self.router.add_api_route("job", self.job, methods=["POST"])
        self.router.on_startup.append(self.job)

    def tick(self):
        print('Tick! The time is: %s' % datetime.now())

    def job(self):
        sched.add_job(self.tick, 'interval', seconds=3)
        sched.start()

        try:
            while True:
                time.sleep(2)
        except (KeyboardInterrupt, SystemExit):
            sched.shutdown()

app.include_router(Router().router)
service.mount_asgi_app(app)

If I use FastAPI, we see the scheduled task result

uvicorn code:app

We can see the scheduled task Tick! The time is: 2022-11-01 22:47:51.878470

Otherwise if we run it with bentoml, we can not schdule the tick method. It does not print Ticks. bentoml serve code:service

To reproduce

No response

Expected behavior

Ticks should bre printed using bentoml.

Environment

bentoml: 1.0.8 pytohn: 3.8

ssheng commented 2 years ago

Though running scheduled task in a service is not well supported use case. I think you can use FastAPI to achieve scheduled task. In FastAPI, you can add startup and shutdown event to an app. You can schedule the task against a BackgroundScheduler in the startup and shutdown the scheduler in the shutdown event. https://fastapi.tiangolo.com/advanced/events/

mathbr10 commented 2 years ago

@ssheng it is perfecly running iwth fastapi, The problem is that it is not running with bentoml, and that is the problem to me because I have a bento app that mounts a fastapi app