Lightning-AI / LitServe

Lightning-fast serving engine for any AI model of any size. Flexible. Easy. Enterprise-scale.
https://lightning.ai/docs/litserve
Apache License 2.0
2.51k stars 160 forks source link

Feat: track number of active requests #325

Closed aniketmaurya closed 1 month ago

aniketmaurya commented 1 month ago
Before submitting - [ ] Was this discussed/agreed via a Github issue? (no need for typos and docs improvements) - [ ] Did you read the [contributor guideline](https://github.com/Lightning-AI/pytorch-lightning/blob/main/.github/CONTRIBUTING.md), Pull Request section? - [ ] Did you make sure to update the docs? - [ ] Did you write any new necessary tests?

What does this PR do?

Addresses - https://github.com/Lightning-AI/LitServe/pull/316#issuecomment-2394987019

import litserve as ls
from litserve import LitAPI

class RequestTracker(ls.Callback):
    def on_request(self, active_requests: int, **kwargs):
        # do something with active_requests
        print(f"Active requests: {active_requests}", flush=True)

class SimpleLitAPI(ls.LitAPI):
    def setup(self, device):
        self.model = lambda x: x ** 2

    def decode_request(self, request):
        return request["input"]

    def predict(self, x):
        return self.model(x)

    def encode_response(self, output):
        return {"output": output}

if __name__ == '__main__':
    lit_api = SimpleLitAPI()
    server = ls.LitServer(lit_api, callbacks=RequestTracker(), track_requests=True)
    server.run()

PR review

Anyone in the community is free to review the PR once the tests have passed. If we didn't discuss your PR in GitHub issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 96%. Comparing base (ed5b68e) to head (24aceb1). Report is 1 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #325 +/- ## =================================== - Coverage 96% 96% -0% =================================== Files 22 22 Lines 1347 1391 +44 =================================== + Hits 1289 1331 +42 - Misses 58 60 +2 ```