Skip to main content

Python 3 microservice library / framework using asyncio with HTTP, AMQP and AWS SNS+SQS support.

Project description

tomodachi - a lightweight microservices framework with asyncio

https://travis-ci.org/kalaspuff/tomodachi.svg?branch=master https://img.shields.io/pypi/v/tomodachi.svg https://codecov.io/gh/kalaspuff/tomodachi/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/tomodachi.svg

Python 3 microservice framework using asyncio (async / await) with HTTP, RabbitMQ / AMQP and AWS SNS+SQS support for event bus based communication.

Tomodachi is a tiny framework designed to build fast microservices listening on HTTP or communicating over event driven message buses like RabbitMQ, AMQP, AWS (Amazon Web Services) SNS+SQS, etc. It’s designed to be extendable to make use of any type of transport layer available.

Tomodachi (友達) means friends – and since microservices wouldn’t make sense on their own I think they need to be friends with each other. 😍 👬 👭 👫

How do I use this?

Installation via pip 🌮

$ pip install tomodachi

Basic HTTP based service 🌟

import tomodachi
from tomodachi.transport.http import http, http_error


@tomodachi.service
class HttpService(object):
    name = 'http_service'

    # Request paths are specified as regex for full flexibility
    @http('GET', r'/resource/(?P<id>[^/]+?)/?')
    async def resource(self, request, id):
        # Returning a string value normally means 200 OK
        return 'id = {}'.format(id)

    @http('GET', r'/health')
    async def health_check(self, request):
        # Return can also be a tuple, dict or even an aiohttp.web.Response
        # object for more complex responses - for example if you need to
        # send byte data, set your own status code or define own headers
        return {
            'body': 'Healthy',
            'status': 200
        }

    # Specify custom 404 catch-all response
    @http_error(status_code=404)
    async def error_404(self, request):
        return 'error 404'

Run service 😎

$ tomodachi run service.py

Requirements 👍

License 🙋

Offered under the MIT license

Source code 🦄

The latest developer version of tomodachi is available at the GitHub repo https://github.com/kalaspuff/tomodachi

Any questions?

What is the best way to run a tomodachi service?

There is no way to tell you how to orchestrate your infrastructure. Some people may run it containerized in a Docker environment, deployed via Terraform and some may run several services on the same environment, on the same machine. There are no standards and we’re not here to tell you about your best practices.

Are there any more example services?

There are a few examples in the examples folder, including examples to publish events/messages to an AWS SNS topic and subscribe to an AWS SQS queue. There’s also a similar example of how to work with pub-sub for RabbitMQ via AMQP transport protocol.

Why should I use this?

I’m not saying you should, but I’m not saying you shouldn’t. tomodachi is a perfect place to start when experimenting with your architecture or trying out a concept for a new service. It may not have all the features you desire and it may never do.

Should I run this in production?

It’s all still highly experimental and it depends on other experimental projects, so you have to be in charge here and decide for yourself. Let me know if you do however!

Who built this and why?

My name is Carl Oscar Aaro and I’m a coder from Sweden. I simply wanted to learn more about asyncio and needed a constructive off-work project to experiment with – and here we are. 🎉

Changes

0.2.13 (2017-06-20)

  • Type hinted examples and test cases.

  • Shielded function calls for AMQP and SNS+SQS transports to avoid unexpected execution stop.

  • Added version output to tomodachi CLI tool.

  • Additional test cases.

0.2.12 (2017-06-18)

  • Type hinted code base and minor bug fixes for internal functions.

0.2.11 (2017-06-09)

  • Invoker methods can now be called directly without the need to mock the invoker decorator function.

0.2.10 (2017-06-08)

  • Added @functools.wraps decorator to invoked functions of service classes.

0.2.9 (2017-06-06)

  • Added a list of safe modules that may never be removed from the list of already loaded modules. Removing the module ‘typing’ from the list would cause a RecursionError exception since Python 3.6.1.

0.2.8 (2017-05-23)

  • Additional improvements to network connectivity issues to not get stuck in waiting state.

0.2.7 (2017-05-23)

  • Improved SNS+SQS draining / restart when network connectivity has been lost or temporarily suspended. Would improve situations when development machine has been in hibernation.

  • Replaced deprecated logging functions to rid warnings.

0.2.6 (2017-05-22)

  • Support for a “generic” aws dictonary in options that can hold region, access key id and secret to be shared among other AWS resources/services.

  • Updated aiobotocore / botocore dependencies.

  • Gracefully handle and discard invalid SNS/SQS messages not in JSON format.

  • Corrected issue where watched directories with “similar” names as settings would be ignored.

0.2.5 (2017-05-16)

  • Updated issues with function caching due to keepalive when hot reloading in development. Currently disables keepalive entirely.

  • Fixed issue with updated file logging for watcher.

0.2.4 (2017-05-12)

  • Downgraded botocore to meet requirements and to make the installed tomodachi script runnable again.

0.2.3 (2017-05-10)

  • Watcher is now configurable to ignore specific directories dependant on the service. (github: smaaland)

  • Fixed issue where using --config instead of -c would result in a raised exception. (github: smaaland)

0.2.2 (2017-05-04)

  • tomodachi.transport.http has its own Response object that works better with default content types and charsets - examples/http_service.py updated.

  • No automatic conversion will be tried if the returned response of an http method is of bytes type.

0.2.1 (2017-05-03)

  • Improved handling of how charsets and encodings work with aiohttp.

  • Fixed an issue where Content-Type header would always be included twice for aiohttp.web.Response objects.

0.2.0 (2017-05-02)

  • Watcher now only reacts to files with file endings .py, .json, .yml, .html or .html and ignores to look at paths __pycache__, .git, .svn, __ignored__, __temporary__ and __tmp__.

  • HTTP transport may now respond with an aiohttp.web.Response object for more complex responses.

  • HTTP transport response headers can now use the multidict library.

0.1.11 (2017-04-02)

  • Working PyPI release.

  • Added unit tests.

  • Works with aiohttp 2 and aiobotocore 0.3.

  • Service classes must be decorated with @tomodachi.service.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

tomodachi-0.2.13.tar.gz (27.6 kB view hashes)

Uploaded Source

Built Distribution

tomodachi-0.2.13-py3-none-any.whl (36.4 kB view hashes)

Uploaded Python 3

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