Skip to content
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

Add support for app.on_event("startup") and app.on_event("cleanup"). #98

Merged
merged 9 commits into from Oct 9, 2018

Conversation

tomchristie
Copy link
Member

@tomchristie tomchristie commented Oct 9, 2018

Closes #64.

Example:

from starlette.applications import Starlette
from starlette.responses import JSONResponse
import uvicorn

app = Starlette()

@app.route('/')
def homepage(request):
    return JSONResponse({'hello': 'world'})

@app.on_event('startup')
async def startup():
    pass

@app.on_event('cleanup')
async def cleanup():
    print('Cleanup')


if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8000)

Requires the latest version of uvicorn (0.3.11), which adds ASGI lifespan support.


Docs:

Starlette applications can register multiple event handlers for dealing with
code than needs to run before the application starts up, or when the application
is shutting down.

Registering events

These event handlers can either be async coroutines, or regular syncronous
functions.

The event handlers are registered with a decorator syntax, like so:

from starlette.applications import Starlette


app = Starlette()

@app.on_event('startup')
async def open_database_connection_pool():
    ...

@app.on_event('cleanup')
async def close_database_connection_pool():
    ...

Starlette will not start serving any incoming requests until all of the
registered startup handlers have completed.

The cleanup handlers will run once all connections have been closed, and
any in-process background tasks have completed.

Note: The ASGI lifespan protocol has only recently been added to the spec,
and is only currently supported by the uvicorn server. Make sure to use the
latest uvicorn release if you need startup/cleanup support.

Running event handers in tests

You might want to explicitly call into your event handlers in any test setup
or test teardown code.

Alternatively, Starlette provides a context manager that ensures the
lifespan events are called.

from example import app
from starlette.lifespan import LifespanContext
from starlette.testclient import TestClient


def test_homepage():
    with LifespanContext(app):
        # Application 'startup' handlers are called on entering the block.
        client = TestClient(app)
        response = client.get("/")
        assert response.status_code == 200

    # Application 'cleanup' handlers are called on exiting the block.

@tomchristie tomchristie merged commit 139753f into master Oct 9, 2018
@tomchristie tomchristie deleted the lifespan branch October 9, 2018 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Startup / Shutdown events
1 participant