Skip to main content

A async microservice framework for python

Project description

μService

A async microservice framework for Python. Inspired by FastAPI and Nameko. It uses the uvloop event loop instead of the default python asyncio loop.

Requirements

Python 3.10+

Quickstart

Installing using pip:

$ pip install uservice

Create a service example.py:

from uservice import Service


service = Service(name='example')


@service.event_handler('source', 'event')
def handle_event(payload):
    print(payload)

Run the service:

$ uservice run example:service

Usage

Command line

run

$ uservice run --help
Usage: uservice run [OPTIONS] SERVICE

Options:
  --reload           Enable auto-reload.
  --workers INTEGER  Number of worker processes. Not valid with --reload
                     [default: 1]
  --help             Show this message and exit.

Events (Pub-Sub)

At the moment only amqp is supported for events.

Subscribe

Event handlers in uservice require that the kwarg payload is in the function that handles it. Event handlers in uservice support runtime typechecking using pydantic, example:

from uservice import Service
from pydantic import BaseModel


service = Service(name='service')


class Payload(BaseModel):
    foo: int
    bar: int
    

@service.event_handler('source', 'event')
def handle_event(payload: Payload):
    print(payload)

If an event is sent to this event_handler not matching the Payload schema it will raise a ValidationError.

Publish

Event publishing in uservice is handled as a dependency injection. It aslo supports validation of payloads using pydantic, example:

from uservice import Service, EventPublisher, Depends
from typing import Annotated
from pydantic import BaseModel


class Payload(BaseModel):
    foo: int
    bar: str


publisher = EventPublisher(publish_model=Payload)

service = Service(name="service")

@service.event_handler("source", "event")
def handle(
        payload: Payload,
        publish: Annotated[Callable, Depends(publisher)],
):
    publish("event", payload)

RPC

At the moment rpcs only support amqp. Rpcs in uservice supports validation of reponses using pydantic.

from uservice import Service
from pydantic import BaseModel


class Response(BaseModel):
    foo: int
    bar: str


service = Service(name="service")

@service.rpc(response_model=Response)
def method(x: int, b: int):
    return {
        'foo': x,
        'bar': y,
    }

Example of calling a rpc from another service. When calling a rpc only support kwargs.

from uservice import Service, RpcProxy, ServiceProxy, Depends
from typing import Annotated
from pydantic import BaseModel


caller = Service(name="caller")

@service.rpc()
def call(service: Annotated[ServiceProxy, Depends(RpcProxy)]):
    print(service.method(x=2, y=3))

Dependency Injection

uservice uses a dependency injection system which is heavily inspired by FastAPI.

Example dependecy:

from uservice import Service, Depends
from typing import Annotated
from pydantic import BaseModel, BaseSettings


class Payload(BaseModel):
    foo: int
    bar: str


service = Service(name="service")

class Settings(BaseSettings):
    version: str = "1.0.0"


def get_settings():
    return Settings()
        

@service.event_handler("source", "event")
def handle(
        payload: Payload,
        settings: Annotated[Settings, Depends(get_settings)],
):
    print(settings.version, payload)

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

uservice-0.1.2.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

uservice-0.1.2-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file uservice-0.1.2.tar.gz.

File metadata

  • Download URL: uservice-0.1.2.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for uservice-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9e2f49b14883df5b736e86661735437984ee503f4793109730dd0648bcc0c114
MD5 d3d4abf368ba2124b8494e913b9a0167
BLAKE2b-256 96c21e3a2ec271458c418c2141c1d37a99956e01bc85c0e57420c9a53637c2e1

See more details on using hashes here.

File details

Details for the file uservice-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: uservice-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for uservice-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 63daf013982a4f46cd409ee246db84e23bbdcc3005aecfb6aefddf145b8937df
MD5 dcfeefc30e49b260968f1f3384345736
BLAKE2b-256 af5fe24b1bfe2a800601511d6fe82a8d2a9c266c96cb91d5c240c63f2864a2cf

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