Skip to main content

A dependency-injection powered application framework

Project description

Engin 🏎️

codecov


Documentation: https://engin.readthedocs.io/

Source Code: https://github.com/invokermain/engin


Engin is a lightweight application framework powered by dependency injection, it helps you build and maintain large monoliths and many microservices.

Features

The Engin framework gives you:

  • A fully-featured dependency injection system.
  • A robust application runtime with lifecycle hooks and supervised background tasks.
  • Zero boilerplate code reuse across applications.
  • Integrations for other frameworks such as FastAPI.
  • Full async support.
  • CLI commands to aid local development.

Installation

Engin is available on PyPI, install it using your favourite dependency manager:

  • pip install engin
  • poetry add engin
  • uv add engin

Example

A small example which shows some of the features of Engin. This application makes 3 http requests and shuts itself down.

import asyncio
from httpx import AsyncClient
from engin import Engin, Invoke, Lifecycle, OnException, Provide, Supervisor


def httpx_client_factory(lifecycle: Lifecycle) -> AsyncClient:
    # create our http client
    client = AsyncClient()
    # this will open and close the AsyncClient as part of the application's lifecycle
    lifecycle.append(client)
    return client


async def main(
    httpx_client: AsyncClient,
    supervisor: Supervisor,
) -> None:
    async def http_requests_task():
        # simulate a background task
        for x in range(3):
            await httpx_client.get("https://httpbin.org/get")
            await asyncio.sleep(1.0)
        # raise an error to shutdown the application, normally you wouldn't do this!
        raise RuntimeError("Forcing shutdown")

    # supervise the http requests as part of the application's lifecycle
    supervisor.supervise(http_requests_task, on_exception=OnException.SHUTDOWN)


# define our modular application
engin = Engin(Provide(httpx_client_factory), Invoke(main))

# run it!
asyncio.run(engin.run())

With logs enabled this will output:

INFO:engin:starting engin
INFO:engin:startup complete
INFO:httpx:HTTP Request: GET https://httpbin.org/get "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://httpbin.org/get "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: GET https://httpbin.org/get "HTTP/1.1 200 OK"
ERROR:engin:supervisor task 'http_requests_task' raised RuntimeError, starting shutdown
Traceback (most recent call last):
  File "C:\dev\python\engin\src\engin\_supervisor.py", line 58, in __call__
    await self.factory()
  File "C:\dev\python\engin\readme_example.py", line 29, in http_requests_task
    raise RuntimeError("Forcing shutdown")
RuntimeError: Forcing shutdown
INFO:engin:stopping engin
INFO:engin:shutdown complete

Inspiration

Engin is heavily inspired by Uber's Fx framework for Go and the Injector framework for Python.

They are both great projects, go check them out.

Benchmarks

Automated benchmarks for the Engin framework can be viewed here.

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

engin-0.2.0.tar.gz (190.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

engin-0.2.0-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file engin-0.2.0.tar.gz.

File metadata

  • Download URL: engin-0.2.0.tar.gz
  • Upload date:
  • Size: 190.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for engin-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0149e83ae0b87eff712dbf5eae6d02dd19fe833790a0a75503f87545b9858666
MD5 3002e68c9f18b3e9337505ad0bf832e3
BLAKE2b-256 7e6c3490cc5ae6364a35473022567a83f8814ded03bb3df117424ddd1df4cee2

See more details on using hashes here.

File details

Details for the file engin-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: engin-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 37.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for engin-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7709240fe0521d2a0a861c1ff0ac3fc58801345cfd13fa88177ab11c5b7d2eb
MD5 c7d55730b1022fdaf44a0a6e97a3f0ea
BLAKE2b-256 97171ad6e139d53ba54a2c9b259b26ff6d335b7e3b15145907df6c818df7a6b9

See more details on using hashes here.

Supported by

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