Comments (4)
Hi @AlTosterino I think it's done automatically by django
at least my quick test shows this:
from ninja import NinjaAPI
import asyncio
from django.http import HttpResponse, StreamingHttpResponse
api = NinjaAPI()
HTML = """
<!DOCTYPE html>
<html>
<head>
<script>
var eventSource = new EventSource("/api/events");
eventSource.onmessage = function(event) {
console.log(event.data);
document.getElementById("log").innerHTML += event.data + "<br>";
};
</script>
</head>
<body>
<div id="log"></div>
</body>
</html>
"""
@api.get("/html")
def html(request):
return HttpResponse(HTML)
@api.get("/events")
async def get_events(request) -> StreamingHttpResponse:
async def generate_message():
while True:
await asyncio.sleep(1)
yield 'data: {"test": "testvalue"}\n\n'
print("Working...")
return StreamingHttpResponse(
generate_message(),
content_type="text/event-stream",
headers={
"X-Accel-Buffering": "no",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
},
)
from django-ninja.
Hmmm, that's very strange...
For me Working...
is printed forever, look at this log:
Working... 1
Working... 2
Working... 3
Working... 4
Working... 5
Working... 6
- - - I PRESSED CTRL+C HERE TO SHUT DOWN THE SERVER - - -
UVICORN.SERVER | 2024-05-15 09:22:18.565 | INFO | uvicorn.server:shutdown:258 - Shutting down | {}
UVICORN.SERVER | 2024-05-15 09:22:18.565 | INFO | uvicorn.server:shutdown:258 - Shutting down | {}
- - - UVICORN KNOWS THERE ARE CONNECTIONS PENDING - - -
UVICORN.SERVER | 2024-05-15 09:22:18.667 | INFO | uvicorn.server:_wait_tasks_to_complete:296 - Waiting for connections to close. (CTRL+C to force quit) | {}
UVICORN.SERVER | 2024-05-15 09:22:18.667 | INFO | uvicorn.server:_wait_tasks_to_complete:296 - Waiting for connections to close. (CTRL+C to force quit) | {}
Working... 7
Working... 8
Working... 9
Working... 10
- - - I CLOSED THE TAB IN THE BROWSER - - -
UVICORN.SERVER | 2024-05-15 09:22:21.796 | INFO | uvicorn.server:_wait_tasks_to_complete:303 - Waiting for background tasks to complete. (CTRL+C to force quit) | {}
UVICORN.SERVER | 2024-05-15 09:22:21.797 | INFO | uvicorn.server:_wait_tasks_to_complete:303 - Waiting for background tasks to complete. (CTRL+C to force quit) | {}
- - - BUT THE TASK STILL RUNS - - -
Working... 11
Working... 12
Working... 13
Working... 14
Working... 15
I'm using event-source-polyfill
for event source (as I need authorization header) but I'll check with built-in event source when I'll be back at home
from django-ninja.
@vitalik I think I found the issue, on django==4.2.13
StreamingHttpResponse goes indefinetely, on django==5.0.6
it works, so it has to be something with Django itself... 😢
from django-ninja.
https://code.djangoproject.com/ticket/34752
from django-ninja.
Related Issues (20)
- Static Method Not Reflecting Instance-Specific Argument in Dynamically Created ModelSchema Class
- [BUG] `fields_optional="__all__"` causes TypeError on models with a field using the `default` parameter HOT 1
- [BUG] `SmallAutoField` missing from `ninja.orm.fields` HOT 1
- Add Redoc Try It Out feature
- Adding the schema for "unauthorized" to the OpenAPI
- The CSRF header name needs to be configurable HOT 2
- create_schema w/ depth: Support ReverseManyToOneDescriptor
- Make `csrf=True` add `X-CSRFToken` header parameters to OpenAPI spec HOT 2
- Docs not updated? HOT 4
- [BUG] Cannot use Django's Test Client when Ninja TestClient is used in a pytest suite HOT 3
- Allow optional fields to be marked as required in `ModelSchema`
- [BUG] Self-Referencing Schemas are Broken HOT 1
- How to implement a generic Filter HOT 1
- [BUG] URL path recognition error when get request comes first and post request comes later
- can you implement websocket for ninja api.
- Documentation of responses with schemes in openapi_extra
- [BUG] ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard' on python 3.12.4
- [BUG] Endpoint payload cannot be an Annotated Union with Discriminator
- Returning alternate response objects HOT 1
- [BUG] mypy crashes with django-ninja installed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-ninja.