Skip to content

enable asynchronous processing in LitAPI #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025
Merged

Conversation

aniketmaurya
Copy link
Collaborator

@aniketmaurya aniketmaurya commented May 7, 2025

What does this PR do?

Users will be able to make async calls inside the LitAPI methods. To use async methods, users need to set the LitAPI.enable_async to True.

Note that the standard non-async way still works and async is opt-in.

import litserve as ls
+ import async_fn from some_async_lib

class SimpleLitAPI(ls.LitAPI):
-    def decode_request(self, request):
+    async def decode_request(self, request):
+        await async_fn()
        return request["input"]

-    def predict(self, x):
+    async def predict(self, x):
        return x**2

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

if __name__ == "__main__":
-    server = ls.LitServer(SimpleLitAPI())
+    server = ls.LitServer(SimpleLitAPI(enable_async=True))
    server.run(port=8000)
Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

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 🙃

…dling

- Added `enable_async` parameter to `LitAPI` constructor to support async methods.
- Implemented `__post_init__` to validate coroutine methods when async support is enabled.
- Introduced `_async_inject_context` for async context injection.
- Enhanced `SingleLoop` to handle requests asynchronously with `_run_single_loop_with_async`.
- Updated `_process_single_request` to manage async request processing and error handling.
- Updated `get_default_loop` function to include `enable_async` parameter for better async handling.
- Added validation for async streaming and batching conditions.
- Adjusted calls to `get_default_loop` in `LitServer` and `inference_worker` to pass the new parameter.
Copy link

codecov bot commented May 7, 2025

Codecov Report

Attention: Patch coverage is 34.28571% with 46 lines in your changes missing coverage. Please review.

Project coverage is 87%. Comparing base (f2237f5) to head (f8286cf).
Report is 1 commits behind head on main.

Additional details and impacted files
@@         Coverage Diff         @@
##           main   #477   +/-   ##
===================================
- Coverage    88%    87%   -2%     
===================================
  Files        37     37           
  Lines      2184   2247   +63     
===================================
+ Hits       1928   1945   +17     
- Misses      256    302   +46     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aniketmaurya aniketmaurya merged commit 005b0ca into main May 7, 2025
21 checks passed
@aniketmaurya aniketmaurya deleted the enable-async-litapi branch May 7, 2025 06:02
@aniketmaurya aniketmaurya mentioned this pull request May 7, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants