Skip to main content

Zero is a RPC framework to build fast and high performance Python microservices

Project description

Zero is a simple Python framework (RPC like) to build fast and high performance microservices or distributed servers


Features:

  • Zero provides faster communication (see benchmarks) between the microservices using zeromq under the hood.
  • Zero uses messages for communication and traditional client-server or request-reply pattern is supported.
  • Support for both Async and sync.
  • The base server (ZeroServer) utilizes all cpu cores.
  • Code generation! See example 👇

Philosophy behind Zero:

  • Zero learning curve: The learning curve is tends to zero. You just add your functions and spin up a server, literally that's it! The framework hides the complexity of messaging pattern that enables faster communication.
  • ZeroMQ: An awesome messaging library enables the power of Zero.

Let's get started!

Getting started 🚀

Ensure Python 3.8+

pip install zeroapi

For Windows, tornado needs to be installed separately (for async operations). It's not included with zeroapi because for linux and mac-os, tornado is not needed as they have their own event loops.

  • Create a server.py
from zero import ZeroServer

app = ZeroServer(port=5559)

@app.register_rpc
def echo(msg: str) -> str:
    return msg

@app.register_rpc
async def hello_world() -> str:
    return "hello world"


if __name__ == "__main__":
    app.run()

Please note that server RPC methods are type hinted. Type hint is must in Zero server.

See the method type async or sync, doesn't matter. 😃

  • Run it
python -m server
  • Call the rpc methods
from zero import ZeroClient

zero_client = ZeroClient("localhost", 5559)

def echo():
    resp = zero_client.call("echo", "Hi there!")
    print(resp)

def hello():
    resp = zero_client.call("hello_world", None)
    print(resp)


if __name__ == "__main__":
    echo()
    hello()

Or using async client -

import asyncio

from zero import AsyncZeroClient

zero_client = AsyncZeroClient("localhost", 5559)

async def echo():
    resp = await zero_client.call("echo", "Hi there!")
    print(resp)

async def hello():
    resp = await zero_client.call("hello_world", None)
    print(resp)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(echo())
    loop.run_until_complete(hello())

Code Generation! 🙌

You can also use our code generation tool to generate Python client code!

After running the server, like above, you can call the server to get the client code.

Using zero.generate_client you can generate client code for even remote servers using the --host and --port options. You don't need access to the code 😃

python -m zero.generate_client --host localhost --port 5559 --overwrite-dir ./my_client

It will generate client like this -

import typing  # remove this if not needed
from typing import List, Dict, Union, Optional, Tuple  # remove this if not needed
from zero import ZeroClient


zero_client = ZeroClient("localhost", 5559)


class RpcClient:
    def __init__(self, zero_client: ZeroClient):
        self._zero_client = zero_client

    def echo(self, msg: str) -> str:
        return self._zero_client.call("echo", msg)

    def hello_world(self, msg: str) -> str:
        return self._zero_client.call("hello_world", msg)

Use the client -

from my_client import RpcClient, zero_client

client = RpcClient(zero_client)

if __name__ == "__main__":
    client.echo("Hi there!")
    client.hello_world(None)

Currently, the code generation tool supports only ZeroClient and not AsyncZeroClient.

Important notes 📝

  • ZeroServer should always be run under if __name__ == "__main__":, as it uses multiprocessing.
  • The methods which are under register_rpc() in ZeroServer should have type hinting, like def echo(msg: str):

Let's do some benchmarking 🤘

Zero is talking about inter service communication. In most real life scenarios, we need to call another microservice.

So we will be testing a gateway calling another server for some data. Check the benchmark/dockerize folder for details.

There are two endpoints in every tests,

  • /hello: Just call for a hello world response 😅
  • /order: Save a Order object in redis

Compare the results! 👇

Benchmarks 🏆

11th Gen Intel® Core™ i7-11800H @ 2.30GHz, 8 cores, 16 threads, 16GB RAM (Docker in Ubuntu 22.04.2 LTS)

(Sorted alphabetically)

Framework "hello world" (req/s) 99% latency (ms) redis save (req/s) 99% latency (ms)
aiohttp 14391.38 10.96 9470.74 12.94
aiozmq 15121.86 9.42 5904.84 21.57
fastApi 9590.96 18.31 6669.81 24.41
sanic 18790.49 8.69 12259.29 13.52
zero(sync) 24805.61 4.57 16498.83 7.80
zero(async) 22716.84 5.61 17446.19 7.24

Todo list 📃

  • Add pydantic support
  • Code generation for pydantic models
  • Improve error handling
  • Fault tolerance

Contribution

Contributors are welcomed 🙏

Please leave a star ⭐ if you like Zero!

"Buy Me A Coffee"

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

zeroapi-0.3.51.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

zeroapi-0.3.51-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file zeroapi-0.3.51.tar.gz.

File metadata

  • Download URL: zeroapi-0.3.51.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for zeroapi-0.3.51.tar.gz
Algorithm Hash digest
SHA256 3c3772eada03f9fc74fc3d8b95e9c72054df081e285227ae72ce38e49bab2a4b
MD5 c826771215a20a3ddcf1411837daddf1
BLAKE2b-256 b1f9f64dc73c64e5662c5a35898801f2393ebe8ae74cc693dc92cf86d183b4e5

See more details on using hashes here.

File details

Details for the file zeroapi-0.3.51-py3-none-any.whl.

File metadata

  • Download URL: zeroapi-0.3.51-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for zeroapi-0.3.51-py3-none-any.whl
Algorithm Hash digest
SHA256 70a47faf655b7344ca33d5508e9b1efae3b804561080534a2f5242b2e9287a84
MD5 da7af8cdef7d5538020569b9a6dd7032
BLAKE2b-256 a5873d5333ec6fbf1710df59feb7d0fd123c3286c18b8f8aceb49ba4bf6743cf

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