from typing import AsyncGenerator
from django.views.generic import View
from django.http import Http404, HttpRequest, StreamingHttpResponse

cleaned_up = False


class TestView(View):
    def get(self, request) -> StreamingHttpResponse:
        async def stream() -> AsyncGenerator[bytes]:
            global cleaned_up
            try:
                for i in range(10):
                    yield f"{i}\n".encode()
            except GeneratorExit:
                print("cleaned up")
                cleaned_up = True

        response = StreamingHttpResponse(stream(), status=200)
        response["Content-Type"] = "text/plain"
        return response
