Skip to main content

rap(par[::-1]) is a simple and fast python async rpc

Project description

pait

rap(par[::-1]) is a simple and fast python async rpc

Refer to the design of aiorpc

Installation

pip install rap

Usage

Server

import asyncio

from rap.server import Server


def sync_sum(a: int, b: int):
    return a + b


def async_sum(a: int, b: int):
    return a + b


async def async_gen(a):
    for i in range(a):
        yield i


loop = asyncio.new_event_loop()
rpc_server = Server()
rpc_server.register(sync_sum)
rpc_server.register(async_sum)
rpc_server.register(async_gen)
server = loop.run_until_complete(rpc_server.create_server())

try:
    loop.run_forever()
except KeyboardInterrupt:
    server.close()
    loop.run_until_complete(server.wait_closed())

Client

import asyncio

from rap.client import Client


client = Client()


def sync_sum(a: int, b: int) -> int:
    pass


# in register, must use async def...
@client.register
async def async_sum(a: int, b: int) -> int:
    pass


# in register, must use async def...
@client.register
async def async_gen(a: int):
    yield


async def main():
    print(f"sync result: {await client.call(sync_sum, 1, 2)}")
    print(f"sync result: {await client.call_by_text('sync_sum', 1, 2)}")

    # client.register
    print(f"async result: {await async_sum(1, 3)}")
    async for i in async_gen(10):
        print(f"async gen result:{i}")


loop = asyncio.get_event_loop()
loop.run_until_complete(client.connect())
loop.run_until_complete(main())
client.close()

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

rap-0.1.0.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

rap-0.1.0-py3-none-any.whl (14.0 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