Rapid RPC Framework for your Python services using Asyncio + MsgPack
Project description
⚡️electron
Rapid RPC Framework for building Python 3.7+ services using Asyncio + MsgPack
Installation
$ pip install electron-rpc
How does it work?
Create a server instance with a single endpoint sum()
which outputs the sum of two values:
from electron.server import Server
app = Server()
@app.endpoint("sum")
async def sum(a, b, **kwargs):
return f"Result: {a + b}"
app.run()
__ __
___ / /__ _____/ /__________ ____
/ _ \/ / _ \/ ___/ __/ ___/ __ \/ __ \
/ __/ / __/ /__/ /_/ / / /_/ / / / /
\___/_/\___/\___/\__/_/ \____/_/ /_/
⚡ electron build v0.0.1-beta
Listening on 127.0.0.1:9999
Registered Endpoints:
- sum
Calling the endpoint from a client
electron uses MsgPack-RPC, so craft a payload and send it over TCP:
import socket
from electron.messages import RemoteProcedureCall
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 9999))
sock.send(RemoteProcedureCall(endpoint="sum", args=[1, 2]).encode())
data = sock.recv(1024)
print(f"Received: \n{data.decode()}")
sock.close()
Received:
Result: 3
Background Tasks
To create a background task, decorate it with @app.background(time_in_seconds)
:
@app.background(3)
async def some_background_task():
print("Running background task every 3 seconds")
Now the task will execute every 3 seconds.
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
electron-rpc-0.0.2.tar.gz
(4.6 kB
view hashes)
Built Distribution
Close
Hashes for electron_rpc-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51860593b72a872796daaa373494ee3a6021221032031bfc22df9edaf36edcc1 |
|
MD5 | fce91172111cf4c04d70899a529e737e |
|
BLAKE2b-256 | a3cc70d22fff2b0a0701194fe2a61e6f3b1229f4478b909832527085c517ab73 |