Skip to content

Add support for non-blocking HTTP handlers #4

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rschlaikjer
Copy link
Contributor

@rschlaikjer rschlaikjer commented Apr 25, 2025

The current design of the HTTP server requires that the HTTP request finishes completely before any other sockets / their requests can be serviced.

For applications where processing an HTTP request may require a (slow) call to an external service, this means that the latency for concurrent calls stacks:

    def HandleHttpRequest(self, request: HttpRequest, **kwargs):
        time.sleep(3)
        return HttpResponse(request, statusCode=200)
root@sim:/root# time curl -I localhost:8091 & time curl -I localhost:8091
...
real	0m3.009s
real	0m6.012s

To improve this, add the option for request processors to indicate that the request is processing but not complete yet, allowing the framework to then spin other connections in parallel with the external work:

    def HandleHttpRequest(self, request: HttpRequest, **kwargs):
        if not request.userState:
            request.userState = time.time() + 3

        if time.time() < request.userState:
            raise HttpResponseProcessing

        return HttpResponse(request, statusCode=200)
root@sim:/root# time curl -I localhost:8091 & time curl -I localhost:8091
...
real	0m3.009s
real	0m3.010s

@rschlaikjer rschlaikjer requested a review from ziyan April 25, 2025 05:41
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.

1 participant