Giter Site home page Giter Site logo

Comments (4)

vitalik avatar vitalik commented on September 27, 2024

Hi @AlTosterino I think it's done automatically by django

at least my quick test shows this:

SCR-20240515-lbvw
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.

AlTosterino avatar AlTosterino commented on September 27, 2024

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.

AlTosterino avatar AlTosterino commented on September 27, 2024

@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.

AlTosterino avatar AlTosterino commented on September 27, 2024

https://code.djangoproject.com/ticket/34752

from django-ninja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.