question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

accessing app state on startup/shutdown handlers

See original GitHub issue

i have a starlette app that uses @app.[route|on_event] decorators for routing but would like to switch to the recommended Starlette(routes=..., on_startup=...).

one thing i’m confusing about: how can i access app state via startup/shutdown handlers? currently i have the following:

from my_middleware import GetDatabaseConnectionMiddleware

app = Starlette()
# some middleware that gets a connection from app.state.connection_pool
# and adds it to the request's scope
app.add_middleware(GetDatabaseConnectionMiddleware)

@app.on_event("startup")
async def startup():
    app.state.connection_pool = await asyncpg.create_pool(
        dsn=DATABASE_URL, min_size=2, max_size=2
    )

i think this is a bit messy but seemed like the most obvious way to set app state in the startup handler. i’m not sure how i’d do the same when app is created after the functions are defined, though. any ideas on how to do this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tomchristiecommented, Jan 14, 2020

i’m not sure how i’d do the same when app is created after the functions are defined, though. any ideas on how to do this?

You can define the startup function before the application instantation. That’s fine, since it won’t run until the app is in scope.

async def startup():
    app.state.connection_pool = await asyncpg.create_pool(
        dsn=DATABASE_URL, min_size=2, max_size=2
    )

app = Starlette(routes=..., middleware=..., on_startup=[startup])
1reaction
tomchristiecommented, Feb 24, 2020

@laurentS Yup I think that’s reasonable. I’ve got an outstanding PR for a context-managed. lifespan alternative to startup/shudown, and yes, that happens to include the app instance… https://github.com/encode/starlette/pull/799

Read more comments on GitHub >

github_iconTop Results From Across the Web

Events: startup - shutdown - FastAPI
Here, the shutdown event handler function will write a text line "Application shutdown" to a file log.txt . Info. In the open() function,...
Read more >
How to: Log Messages When the Application Starts or Shuts ...
On the Declarations menu, choose Shutdown. The application raises the Shutdown event after the main application runs, but before it shuts down.
Read more >
Managing your app's life cycle - Apple Developer
The current state of your app determines what it can and can't do at any time. For example, a foreground app has the...
Read more >
tiangolo/fastapi - Gitter
Event() def shutdown(self): self.cancellation_event.set() async def ... service_task = asyncio.create_task(service.startup()) app.state.service = service ...
Read more >
Understanding Server Life Cycle - Oracle Help Center
Perform day-to-day operations tasks, including startup and shutdown procedures; Diagnose problems with application services; Plan corrective actions, such as ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found