Skip to main content

Fast, simple microservice framework

Project description

FastMicro

Fast, simple microservice framework

Test Coverage Package version


FastMicro is a modern, fast (high-performance) framework for building microservices with Python 3.7+ based on asyncio.

Install

To install FastMicro run the following:

$ pip install fastmicro[redis]

Example

This example shows how to use the default in-memory backend for evaluation and testing.

Note:

The in-memory backend cannot be used for inter-process communication.

Create it

  • Create a file hello.py with:
#!/usr/bin/env python3

import asyncio
from pydantic import BaseModel

from fastmicro.messaging.memory import Messaging
from fastmicro.service import Service


class User(BaseModel):
    name: str


class Greeting(BaseModel):
    name: str
    greeting: str


service = Service("test")
loop = asyncio.get_event_loop()
messaging = Messaging(loop=loop)
user_topic = messaging.topic("user", User)
greeting_topic = messaging.topic("greeting", Greeting)


@service.entrypoint(user_topic, greeting_topic)
async def greet(user: User) -> Greeting:
    greeting = Greeting(name=user.name, greeting=f"Hello, {user.name}!")
    return greeting


async def main() -> None:
    await service.start()

    async with messaging:
        user = User(name="Greg")
        print(user)
        greeting = await service.greet(user)
        print(greeting)

    await service.stop()


if __name__ == "__main__":
    loop.run_until_complete(main())

Run it

$ python hello.py
{'name': 'Greg'}
{'name': 'Greg', 'greeting': 'Hello, Greg!'}

Backends

FastMicro supports the following backends:

To install FastMicro with one of these backends run one of the following:

$ pip install fastmicro[kafka]
$ pip install fastmicro[redis]

Another Example

This example shows how to use the Redis backend for inter-process communication.

Create it

  • Create a file example.py with:
#!/usr/bin/env python3

import asyncio
from pydantic import BaseModel

from fastmicro.messaging.redis import Messaging
from fastmicro.service import Service


class User(BaseModel):
    name: str


class Greeting(BaseModel):
    name: str
    greeting: str


service = Service("test")
loop = asyncio.get_event_loop()
messaging = Messaging(loop=loop)
user_topic = messaging.topic("user", User)
greeting_topic = messaging.topic("greeting", Greeting)


@service.entrypoint(user_topic, greeting_topic)
async def greet(user: User) -> Greeting:
    print(user)
    greeting = Greeting(name=user.name, greeting=f"Hello, {user.name}!")
    print(greeting)
    return greeting


if __name__ == "__main__":
    service.run()
  • Create a file test.py with:
#!/usr/bin/env python3

import asyncio
from pydantic import BaseModel

from fastmicro.messaging.redis import Messaging
from fastmicro.service import Service


class User(BaseModel):
    name: str


class Greeting(BaseModel):
    name: str
    greeting: str


service = Service("test")
loop = asyncio.get_event_loop()
messaging = Messaging(loop=loop)
user_topic = messaging.topic("user", User)
greeting_topic = messaging.topic("greeting", Greeting)


@service.entrypoint(user_topic, greeting_topic)
async def greet(user: User) -> Greeting:
    ...


async def main() -> None:
    async with messaging:
        user = User(name="Greg")
        print(user)
        greeting = await service.greet(user)
        print(greeting)


if __name__ == "__main__":
    loop.run_until_complete(main())

Run it

  • In a terminal run:
$ python example.py
{'name': 'Greg'}
{'name': 'Greg', 'greeting': 'Hello, Greg!'}
^C
  • In another terminal run:
$ python test.py
{'name': 'Greg'}
{'name': 'Greg', 'greeting': 'Hello, Greg!'}

License

This project is licensed under the terms of the MIT license.

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

fastmicro-0.3.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

fastmicro-0.3.2-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file fastmicro-0.3.2.tar.gz.

File metadata

  • Download URL: fastmicro-0.3.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.4.0-91-generic

File hashes

Hashes for fastmicro-0.3.2.tar.gz
Algorithm Hash digest
SHA256 d0bb8481e8a564ce3f5e66e238fa92f6b757c7e85b027071b5a039bcede3eedc
MD5 ff2b77c382b2e66504f3deefcfbef060
BLAKE2b-256 016cba848a2ad7076bb31636ac1fe0e1a3e31bf4ee18ccb275f01ea97e150625

See more details on using hashes here.

File details

Details for the file fastmicro-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: fastmicro-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.10 Linux/5.4.0-91-generic

File hashes

Hashes for fastmicro-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cafc4c23bd80e805b9aea14e3905152ee14934796b9a6d657fbb73d488119399
MD5 f6f88ecc5fea17410f855da67f7a609f
BLAKE2b-256 a3acf94ea5f8ad67d62e2b3118777e25cf68281ce667fc6d82cb82f9e60c7009

See more details on using hashes here.

Supported by

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