Code Monkey home page Code Monkey logo

Comments (6)

euri10 avatar euri10 commented on June 2, 2024

the issue and patch seems legit to me, can you submit a PR @peterschutt ?

from uvicorn.

peterschutt avatar peterschutt commented on June 2, 2024

Thanks @euri10 - I'll polish up the patch and submit a PR.

from uvicorn.

Kludex avatar Kludex commented on June 2, 2024

I don't think this is a bug. My interpretation from the spec is that once the "lifespan.startup.complete" is sent, your application is the only task running.

Also, this is the same behavior as the other ASGI servers.

I think what you want is something like this:

import anyio
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Route

import uvicorn


async def infinite_task():
    while True:
        await anyio.sleep(1)
        print("I am alive!")


async def health(request: Request):
    return JSONResponse({"status": "ok"})


app = Starlette(routes=[Route("/health", health)])


async def main():
    async with anyio.create_task_group() as tg:
        tg.start_soon(infinite_task)

        config = uvicorn.Config(app=app, host="0.0.0.0", port=8002)
        server = uvicorn.Server(config=config)
        await server.serve()


if __name__ == "__main__":
    anyio.run(main, backend_options={"use_uvloop": True})

from uvicorn.

peterschutt avatar peterschutt commented on June 2, 2024

Thanks @Kludex - that pattern will work just fine!

once the "lifespan.startup.complete" is sent, your application is the only task running.

Do you feel there is a disconnect between the spec, and how some frameworks advertise lifespan? For example, from starlette's docs (ref):

Consider using anyio.create_task_group() for managing asynchronous tasks.

And from litestar (ref):

This can be useful when dealing with long running tasks

And lastly, from a framework perspective, can we do any better than returning the "lifespan.shutdown.failed" message, in the case where an exception is caught within the lifespan?

from uvicorn.

Kludex avatar Kludex commented on June 2, 2024

Do you feel there is a disconnect between the spec, and how some frameworks advertise lifespan? For example, from starlette's docs (ref):

I'm not sure.

And lastly, from a framework perspective, can we do any better than returning the "lifespan.shutdown.failed" message, in the case where an exception is caught within the lifespan?

I don't know.

from uvicorn.

peterschutt avatar peterschutt commented on June 2, 2024

@euri10 can we close this please?

from uvicorn.

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.