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 everything from large monoliths to hundreds of 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.1.tar.gz (30.1 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.1-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for engin-0.2.1.tar.gz
Algorithm Hash digest
SHA256 dfe898fd511439079f2fb37234a0236dccf6709f46c7c2ef89ca6c0968af52f5
MD5 98ce9a834cc92951da3b1d78af0e1446
BLAKE2b-256 6b34d9394ee81a60a218269dfd3c5e58dd4230cc4e77f2f28d2e5d27f677adfb

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for engin-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d42edfe6d5f3d9f419886416a6d92c778bf9e268d84100b7957bf3de1f9872be
MD5 a1012bbdf3b3df1cbebcfed38568077f
BLAKE2b-256 830a7dfde77304ce078380fbaafc2380d4649a6e2c755464ac160314a0494ed7

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