v0.0.5 Lifespan / Lifecycle events
What is changed in socketify:
- Small fix on
res.send
andres.cork_send
- Lifespan / Lifecycle events
Lifespan / Lifecycle events
Now you can use socketify start and shutdown events to create/clean thread pools, connections pools, etc when the application starts or shutdown itself.
If any exception occurs in the start event the application will continue and start normally,
if you want to fail a start you need to catch the exception and use sys.exit(1)
to shut down prematurely.
Both app.on_start
and app.on_shutdown
can be sync or async.
from socketify import App
def run(app: App)
@app.on_start
async def on_start():
print("wait...")
await asyncio.sleep(1)
print("start!")
@app.on_shutdown
async def on_shutdown():
print("wait...")
await asyncio.sleep(1)
print("shutdown!")
router = app.router()
@router.get("/")
def home(res, req):
res.send("Hello, World!")