Try gRPC in Python

Install gRPC

First, install gRPC. It seems that Protocol Buffers, which is responsible for serializing RPC, will be installed along with gRPC.

$ curl -fsSL https://goo.gl/getgrpc | bash -s python

If you get the following error during installation, homebrew is out of date and you should try brew update. (For Mac)

Error: undefined method `desc' for Grpc:Class
Please report this bug:
    http://git.io/brew-troubleshooting
/usr/local/Library/Taps/grpc/homebrew-grpc/Formula/grpc.rb:2:in `<class:Grpc>'
・ ・ ・

Write .proto (IDL)

Write the IDL for Protocol Buffers. The following example defines an RPC that drives the servo.

gRPC itself is a plugin for Protocol Buffers. (There are many [RPC implementations] besides gRPC (https://github.com/google/protobuf/wiki/Third-Party-Add-ons#rpc-implementations))

gateway.proto


syntax = "proto3";

package gateway;

message MoveServoRequest {
  int32 servo_id = 1;
  int32 position = 2;
}

message MoveServoResponse {
  int32 current_position = 1;
}

service AVRGateway {
  rpc MoveServo (MoveServoRequest) returns (MoveServoResponse) {}
}

Compile IDL to generate gRPC python script

The following protoc command will generate gateway_pb2.py.

$ protoc --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` gateway.proto

Write server / client code for gRPC

You can easily write a server / client by importing the Python script generated by the above command. Since the gRPC Python library itself is still Alpha version, various methods are prefixed with early_adopter_ *.

gateway_server.py


import time
import gateway_pb2

class AVRGateway(gateway_pb2.EarlyAdopterAVRGatewayServicer):
  def MoveServo(self, request, context):
    print 'servo_id: %d, position: %d' % (request.servo_id, request.position)
    return gateway_pb2.MoveServoResponse(current_position=150)

def serve():
  server = gateway_pb2.early_adopter_create_AVRGateway_server(AVRGateway(), 9494, None, None)
  server.start()
  try:
    while True:
      time.sleep(100000)
  except KeyboardInterrupt:
    server.stop()

if __name__ == '__main__':
  serve()

gateway_client.py


import gateway_pb2

def run():
  with gateway_pb2.early_adopter_create_AVRGateway_stub('localhost', 9494) as stub:
    response = stub.MoveServo(gateway_pb2.MoveServoRequest(servo_id=1, position=200), 10) 
    print "current position: " + str(response.current_position)

if __name__ == '__main__':
  run()

Run

First, start the server.

$ ls
gateway.proto      gateway_client.py  gateway_pb2.py      gateway_server.py
$ python gateway_server.py

And the client in another terminal.

$ python gateway_client.py
current position: 150
D0729 10:42:14.835677000 140735135564544 iomgr.c:119] Waiting for 1 iomgr objects to be destroyed and executing final callbacks

On the server side

$ python gateway_server.py 
servo_id: 1, position: 200

If it is output like that, it's OK.

Recommended Posts

Try gRPC in Python
Simple gRPC in Python
Try 9 slices in Python
Try python
Try LINE Notify in Python
Try implementing Yubaba in Python 3
Try implementing extension method in python
Try using LevelDB in Python (plyvel)
Try using Leap Motion in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Python> try: / except:
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Try logging in to qiita with Python
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try working with binary data in Python
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Quickly try Microsoft's Face API in Python
Try text mining your diary in Python
Try hitting the YouTube API in Python
Try a functional programming pipe in Python
Try something like Python for-else in Ruby
Sorted list in Python