Skip to main content

The Starlette events bridge that you need.

Project description

Starlette Bridge

Starlette Bridge

🚀 The Starlette events bridge that you need. 🚀

Test Suite Package version Supported Python versions


Documentation: https://starlette-bridge.tarsild.io 📚

Source Code: https://github.com/tarsil/starlette-bridge


Motivation

Starlette has evolved and it will keep on growing. In the release 0.26+ it was announced the on_startup and on_shutdown events would be removed in favour of the newly lifepsan and those would be officially removed in the release 1.0.

The problem is the fact that there are a lof od packages out there still using the old ways of using the events and this change will introduce breaking changes.

This bridge will make sure that doesn't happen and you can still use the old form of using the on_startup and on_shutdown without breaking the lifespan from Starlette.

What Starlette Bridge does for you is simple. It gets the on_startup and on_shutdown events in the same fashion you could do before and internally generates the newly lifespan for starlette.

That simple. Keeping the old syntax intact and using the newly lifespan.

Installation

To install Starlette Bridge, simply run:

$ pip install starlette-bridge

How to use

This is actually very simple to do it. You don't need to do anything particularly difficult, in fact, you only need to update where your Starlette object comes from.

from starlette_bridge import Starlette

app = Starlette()

And that is pretty much it.

How does it work

Starlette bridge simply maps your on_startup and on_shutdown events and converts them into the new lifespan async generator from Starlette.

This way you can continue to use your preferred way of assembling the events while maintaining the new structure required by Starlette for managing events.

on_event and add_event_handler

These two pieces of functionality are also supported by the bridge making sure that what you had in the past, still remains working as is without changing the syntax.

Let us see an example how it works. We will be using Starlette Bridge because already contains events we want to use.

on_startup/on_shutdown

Using the on_startup and on_shutdown.

from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


app = Starlette(
    on_startup=[database.connect],
    on_shutdown=[database.disconnect],
)

Lifespan

You can, of course, use the lifespan as well.

from contextlib import asynccontextmanager

from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


@asynccontextmanager
async def lifespan(app: Starlette):
    # On startup
    await database.connect()
    yield
    # On shutdown
    await database.disconnect()


app = Starlette(lifespan=lifespan)

on_event and add_event_handler

As mentioned before, those two functionalities are also available.

on_event
from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


app = Starlette()


@app.on_event("startup")
async def start_database():
    await database.connect()


@app.on_event("shutdown")
async def close_database():
    await database.disconnect()
add_event_handler
from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


app = Starlette()
app.add_event_handler("startup", database.connect)
app.add_event_handler("shutdown", database.disconnect)

Notes

This is from the same author of Esmerald, Starlette Bridge and Asyncz. Have a look around those techologies as well 😄.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_bridge-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

starlette_bridge-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file starlette_bridge-0.1.0.tar.gz.

File metadata

  • Download URL: starlette_bridge-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for starlette_bridge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bb49f9aea8b38b0889fa7517cc1c97b4f3f7432becca3417651e8442cabd4030
MD5 070d733d393863dde09f488ed0303f8e
BLAKE2b-256 6b9aa42c289921ab4ae2c2f1a19609d8f2edcca77ba6f05af1ed7c5c54bf8e44

See more details on using hashes here.

File details

Details for the file starlette_bridge-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_bridge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 84fa483725a12db748c42b189fd8472087b3bcf5482c6000ce73309eea6d91f7
MD5 0d54cabe9b0b5a7a5a88b3bdc6743064
BLAKE2b-256 1cbbfa2c2e029d3d8b7b9f0aad1fadf1e017532023b3c2ba2106134d51ded569

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page