Create microservices with celery and abstract interfaces.
Project description
celery-abc
It's time for some dynamic meta-magic!
What is it?
Set of tools to create microservice app with celery
as message brocker.
When to use it?
Say, you want to write some microservices with python. And you want them to work seamlessly with each other - call methods of one service from another like it is a single app.
You may use celery
just for that. But you actually need to register tasks in the same app. Isn't that a little strange - to keep the code base of ALL the services in each and every of them?
Wouldn't it be more convinient, to share only interfaces? That way each developer could track what they can use of other services, but without actully storing their code.
How to use it?
We'll need at least two services and a shared directory. In shared directory we'll create an abstract class Interface
. Let's call one service Caller
and the other - Worker
. Caller
has Interface
imported, but knows nothing about the implementation. Worker
has Interface
implemented.
- Create common directory, share it with all your services. Chances are - you'll need one anyway.
- In the shared folder create python module. Define your interfaces there. For now,
celery-abc
requires forInterface
to be abstract.
from abc import ABC, abstractmethod
class Interface(ABC):
@abstractmethod
def do_some_stuff(self, arg):
pass
@abstractmethod
def do_more_stuff(self, arg):
pass
- In your
Worker
service importInterface
from shared module and implement it. Metaclass for your implementation should beWorkerMetaBase
fromcelery-abc
. You don't need to register it's methods, just instantiate your class withcelery
. Run yourworker
as a celery worker.
from celery import Celery
from celery_abc import WorkerMetaBase
from shared import Interface
class Worker(Interface, metaclass=WorkerMetaBase):
def do_some_stuff(self, arg):
return self.do_more_stuff(arg)
def do_more_stuff(self, arg):
return f"Doing stuff {arg}"
celery_app = Celery(...)
Worker(celery)
- In
Caller
service importCallerMetaBase
, create a new class, that inherits fromInterface
and haveCallerMetaBase
as a metaclass. Instantiate it with yourcelery
app. Unlikeworker
,caller
may run just as regular python app. Now, you can call methods, that are implemented in theWorker
=)
class Caller(Interface, metaclass=CallerMetaBase):
pass
celery_app = Celery(...)
caller = Caller(celery)
result = caller.do_some_stuff('Hello!')
Here you go, now you can seamlessly call methods of Worker
from Caller
. As you can see, you can call Worker
's methods from each other. But you can not keep any instnce variables in Worker
's self
. In the end, microservices are better when they are stateless.
When not to use it?
- If you want to create statefull microservices - you still can use
celery-abc
, if only for comunication. But there may be other solutions. - If you want to create complex system of classes. OOP is cool, but multiple and chaind inheritance are not supported at the moment.
- Method signature can't have
*args
and**kwargs
in it. It is totally fine to pass argumetns this way, but capturing them is not supported yet.
Interesting stuff
- Abstract methods
- Metaclasses
- Object-method
- Descriptors
- inspect
- worker's self
Example
Project includes small example module. To run it, create inside config.env
file with redis creds and run docker-compose -f "tests\docker-compose.yaml" up -d --build
RABBITMQ_DEFAULT_USER=...
RABBITMQ_DEFAULT_PASS=...
RABBITMQ_HOST=...
RABBITMQ_PORT=...
In the future:
- Support inheritance
- Support capturing arguments
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
Built Distribution
File details
Details for the file celery-abc-0.0.1.tar.gz
.
File metadata
- Download URL: celery-abc-0.0.1.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a691504bb1f599aa7f5cc17816a22fe1b2053961e351605f3fe3aa7604535e10 |
|
MD5 | df6913c15b4d75ce4447106f810b6871 |
|
BLAKE2b-256 | 24f0bf3d83af8ab03465ba3f1550bb1edc46542c6c57927d7a9e228d3c1842f8 |
File details
Details for the file celery_abc-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: celery_abc-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb1a7f27f61257c39dd5129e11a918a1b174c1791e3754fa623d0c096d2b2d18 |
|
MD5 | fcc45df3742d76f1db62a45ebb08a6d9 |
|
BLAKE2b-256 | b81beeb68e35260de22c8235d7bcd4a93cafc95b6b0b43a6e52da1835876eafb |