A Lightweight RPC framework for Python
Project description
thinrpc: A Lightweight RPC Framework for Python
===
This library provides a base class and some decorators to make it easy to build distributed applications. RPC endpoints are defined as class methods and decorated with ```RpcModule.Method```, which registers the endpoint with the event-based RPC server dispatching requests to registered methods.
The ```RpcRemote``` class is an abstraction allowing applications to invoke methods on remote nodes using method call syntax. See the example below for an illustration!
```python
'''
Simple RPC server responding to invocations of the
'hello' method with a pleasant greeting
'''
import sys
from thinrpc import RpcModule, RpcRemote, RpcApplication
class FooNode(RpcApplication):
def __init__(self, port, name):
self.addr = ("localhost", port)
self.name = name
# Start the RPC server
self.Start()
@RpcModule.Method
def hello(self, sender):
return "Hi, %s! It's %s" % (sender, self.name)
if __name__ == '__main__':
node = FooNode(9090, "Anson")
```
And now for a simple client script that invokes the ```hello``` method on the server application defined above.
```python
import thinrpc
import sys
# Say hi to the server!
server = thinrpc.RpcRemote(("localhost", 9090))
print(server.hello())
# {'ok': True, 'result': "Hi, ('127.0.0.1', 44222)! It's Anson"}
```
===
This library provides a base class and some decorators to make it easy to build distributed applications. RPC endpoints are defined as class methods and decorated with ```RpcModule.Method```, which registers the endpoint with the event-based RPC server dispatching requests to registered methods.
The ```RpcRemote``` class is an abstraction allowing applications to invoke methods on remote nodes using method call syntax. See the example below for an illustration!
```python
'''
Simple RPC server responding to invocations of the
'hello' method with a pleasant greeting
'''
import sys
from thinrpc import RpcModule, RpcRemote, RpcApplication
class FooNode(RpcApplication):
def __init__(self, port, name):
self.addr = ("localhost", port)
self.name = name
# Start the RPC server
self.Start()
@RpcModule.Method
def hello(self, sender):
return "Hi, %s! It's %s" % (sender, self.name)
if __name__ == '__main__':
node = FooNode(9090, "Anson")
```
And now for a simple client script that invokes the ```hello``` method on the server application defined above.
```python
import thinrpc
import sys
# Say hi to the server!
server = thinrpc.RpcRemote(("localhost", 9090))
print(server.hello())
# {'ok': True, 'result': "Hi, ('127.0.0.1', 44222)! It's Anson"}
```
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
thinrpc-0.1.17.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for thinrpc-0.1.17.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 660ca17d04efa827aef143b214b0890b59c905321252ab8d70f04bef1bf7a67e |
|
MD5 | 56a05d108b8e7d27406c82b148696f4c |
|
BLAKE2b-256 | 8a4d3acd9b6519acdbfee3d8d2a325f292e84b03d6276735b9dac28708164165 |