-
-
Notifications
You must be signed in to change notification settings - Fork 456
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
Adding the event source method to NinjaAPI and Router (SSE) #1388
base: master
Are you sure you want to change the base?
Conversation
Could you also add tests for async views ? the sync mode does not make much sense actually as it can easily occupy all wsgi threads/processes |
@vitalik I need to investigate, when I test with html views it works, but when I run the tests, it fails saying "TypeError: object StreamingHttpResponse can't be used in 'await' expression" I will continue investigating the reason. |
Hello, @vitalik I have added a test to check the asynchronous endpoint. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small nitpick
@api.event_source("/path") | ||
def event_source_operation(request): | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be made async?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it works with async
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My apologies, I meant can we refractor this to make it like, ( because we shouldn't allow users to lock up wsgi workers )
@api.event_source("/path")
async def event_source_operation(request):
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible, but Django's implementation already prevents that blocking, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although a StreamingHttpResponse with an async generator will only work properly if they are using asgi instead of wsgi.
Should we still make async the default in the documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks good, just one small change request
@api.event_source("/path") | ||
def event_source_operation(request): | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My apologies, I meant can we refractor this to make it like, ( because we shouldn't allow users to lock up wsgi workers )
@api.event_source("/path")
async def event_source_operation(request):
...
Also, i have a suggestion, should we allow only async server events? Otherwise we might get complaints in the future about this |
I understand that both should be allowed. Even the StreamingHttpResponse works better when it is synchronous (I imagine it is due to the implementation underneath) |
Good morning @vitalik @baseplate-admin , What changes would be good to merge this PR? Do I put in the documentation that the method is asynchronous but with a warning that it should be used with asgi when it is asynchronous? |
hello @vitalik @baseplate-admin How could we continue with this PR? |
Apologies, i think @vitalik needs to give his final opinion on this PR.
Sounds good to me, add the comment made by vitalik, something like
|
add a note to HTTP operations to inform that SSE can block in WSGI
I just added the notice |
Okay, thank you. @vitalik will make the final call on this |
With this PR I propose to make it easier to use SSE in Django Ninja.