Skip to content

v0.0.5 Lifespan / Lifecycle events

Compare
Choose a tag to compare
@cirospaciari cirospaciari released this 09 Jan 09:41
· 136 commits to main since this release

What is changed in socketify:

  • Small fix on res.send and res.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!")

Work in progress for v0.1.0:

  • Native optimizations #52
  • Cache tools #60
  • Hot reloading #61