Skip to content

Commit

Permalink
Demo service implemented in various frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
hardikp committed Jul 27, 2018
1 parent 16bf68e commit 07c74d3
Show file tree
Hide file tree
Showing 24 changed files with 735 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# service_demo
Simple examples for gRPC, Thrift and RPyC
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/hardikp/service_demo/blob/master/LICENSE)

Python3 server and client examples for [gRPC](https://grpc.io/), [Thrift](https://thrift.apache.org/docs/) and [RPyC](https://rpyc.readthedocs.io/en/latest/).

## Service Example

* `TimeService` implents `get_time` RPC call.
* `get_time` returns the current server time in string format.

## How to use

1. Create an virtualenv and install requirements.
```bash
cd grpc
pip3 install -r requirements.txt
```

1. Run Server in one terminal window:
```bash
python3 server.py
```

1. Run Client in another terminal:
```bash
python3 server.py
15 changes: 15 additions & 0 deletions grpc/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import grpc

import time_pb2
import time_pb2_grpc


def run():
channel = grpc.insecure_channel('localhost:50051')
stub = time_pb2_grpc.TimeStub(channel)
response = stub.GetTime(time_pb2.TimeRequest())
print('Client received: {}'.format(response.message))


if __name__ == '__main__':
run()
3 changes: 3 additions & 0 deletions grpc/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
grpcio
grpcio-tools
googleapis-common-protos
30 changes: 30 additions & 0 deletions grpc/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import time
from concurrent import futures

import grpc

import time_pb2
import time_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class Timer(time_pb2_grpc.TimeServicer):
def GetTime(self, request, context):
return time_pb2.TimeReply(message=time.ctime())


def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
time_pb2_grpc.add_TimeServicer_to_server(Timer(), server)
server.add_insecure_port('[::]:50051')
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)


if __name__ == '__main__':
serve()
15 changes: 15 additions & 0 deletions grpc/time.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";
package time;

service Time {
rpc GetTime (TimeRequest) returns (TimeReply) {}
}

// Empty Request Message
message TimeRequest {
}

// The response message containing the time
message TimeReply {
string message = 1;
}
125 changes: 125 additions & 0 deletions grpc/time_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions grpc/time_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
import grpc

import time_pb2 as time__pb2


class TimeStub(object):
# missing associated documentation comment in .proto file
pass

def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.GetTime = channel.unary_unary(
'/time.Time/GetTime',
request_serializer=time__pb2.TimeRequest.SerializeToString,
response_deserializer=time__pb2.TimeReply.FromString,
)


class TimeServicer(object):
# missing associated documentation comment in .proto file
pass

def GetTime(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_TimeServicer_to_server(servicer, server):
rpc_method_handlers = {
'GetTime': grpc.unary_unary_rpc_method_handler(
servicer.GetTime,
request_deserializer=time__pb2.TimeRequest.FromString,
response_serializer=time__pb2.TimeReply.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'time.Time', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
4 changes: 4 additions & 0 deletions rpyc/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import rpyc

conn = rpyc.connect('localhost', 18871)
print('Time is {}'.format(conn.root.get_time()))
1 change: 1 addition & 0 deletions rpyc/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rpyc
14 changes: 14 additions & 0 deletions rpyc/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import time

from rpyc import Service
from rpyc.utils.server import ThreadedServer


class TimeService(Service):
def exposed_get_time(self):
return time.ctime()


if __name__ == '__main__':
s = ThreadedServer(TimeService, port=18871)
s.start()
38 changes: 38 additions & 0 deletions thrift/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python
import sys

from thrift import Thrift
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket, TTransport
sys.path.append('gen-py')
from time_service import TimeService


def main():
# Make socket
transport = TSocket.TSocket('localhost', 9090)

# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)

# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)

# Create a client to use the protocol encoder
client = TimeService.Client(protocol)

# Connect!
transport.open()

ts = client.get_time()
print('Client Received {}'.format(ts))

# Close!
transport.close()


if __name__ == '__main__':
try:
main()
except Thrift.TException as tx:
print('%s' % tx.message)
Empty file added thrift/gen-py/__init__.py
Empty file.
Loading

0 comments on commit 07c74d3

Please sign in to comment.