Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dvf/synapse
Rapid RPC framework for building Python services fast
https://github.com/dvf/synapse
asyncio flask framework msgpack python python3
Last synced: 26 days ago
JSON representation
Rapid RPC framework for building Python services fast
- Host: GitHub
- URL: https://github.com/dvf/synapse
- Owner: dvf
- Created: 2018-08-07T12:20:33.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-25T22:40:20.000Z (over 5 years ago)
- Last Synced: 2024-09-18T03:34:12.760Z (about 2 months ago)
- Topics: asyncio, flask, framework, msgpack, python, python3
- Language: Python
- Homepage:
- Size: 72.3 KB
- Stars: 12
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
A rapid RPC Framework for building p2p networks
# Installation
```
pip install synapse-p2p
```# How does it work?
This example creates a public endpoints called `sum` which anyone on the network may call. There's also a background task `do_stuff` which is a task running in the background.
```python
from synapse_p2p.server import Serverapp = Server()
@app.background(3)
async def do_stuff():
print("Running background task every 3 seconds")@app.endpoint("sum")
async def my_endpoint(a, b, response, **kwargs):
response.write(f"The sum is {a + b}".encode())app.run()
```## Calling an endpoint on a node from a Python client
Synapse uses MsgPack-RPC, so we craft a payload and send it over TCP:
```python
import socketfrom synapse_p2p.messages import RemoteProcedureCall
from synapse_p2p.serializers import MessagePackRPCSerializersock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 9999))
sock.send(
MessagePackRPCSerializer.serialize(RemoteProcedureCall(endpoint="sum", args=[1, 2]))
)data = sock.recv(1024)
print(f"Received: \n{data.decode()}")
sock.close()
```
```
Received:
Thanks, the solution is 3
```