Python の gRPC で Redis のデータを更新 (Update)

設定ファイル、サーバープログラム、クライアントプログラムの３つが必要です。

設定ファイル

```text:redis_update.proto

syntax = "proto3";

package redis_update;

service Greeter {
	rpc RedisUpdate (RedisRequest) returns (RedisReply) {}
}

message RedisRequest {
	string key = 1;
	int32 population = 2;
}

message RedisReply {
	string key = 1;
}
```

サーバープログラム

```py:redis_update_server.py
#! /usr/bin/python
#
#	redis_update_server.py
#
#					Feb/08/2020
# --------------------------------------------------------------
import sys
import json
import redis
from concurrent import futures
import logging
import	datetime

import grpc

import redis_update_pb2
import redis_update_pb2_grpc

# --------------------------------------------------------------
def redis_update_proc(key_in,population_in):
	rr = redis.Redis(host='localhost', port=6379, db=0)
#
	str_json = rr.get(key_in).decode ()
	unit_aa = json.loads(str_json)
	now = datetime.datetime.now()
	unit_aa['population'] = population_in
	unit_aa['date_mod'] =  '%s' % now
	str_json = json.dumps(unit_aa)
	rr.set(key_in, str_json)
#
# --------------------------------------------------------------
class Greeter(redis_update_pb2_grpc.GreeterServicer):

	def RedisUpdate(self, request, context):
		sys.stderr.write("*** RedisRead ***\n")
		sys.stderr.write("*** key = %s ***\n" % request.key)
		sys.stderr.write("*** population = %d ***\n" % request.population)
		redis_update_proc(request.key,request.population)
		return redis_update_pb2.RedisReply(key=request.key)

# --------------------------------------------------------------
def serve():
	server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
	redis_update_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
	server.add_insecure_port('[::]:50051')
	server.start()
	server.wait_for_termination()

# --------------------------------------------------------------
if __name__ == '__main__':
	logging.basicConfig()
	serve()
# --------------------------------------------------------------
```

クライアントプログラム

```py:redis_update_client.py
#! /usr/bin/python
#
#	redis_update_client.py
#
#					Feb/08/2020
#
# --------------------------------------------------------------
from __future__ import print_function
import logging
import json
import grpc
import sys

import redis_update_pb2
import redis_update_pb2_grpc

# --------------------------------------------------------------
def run():
	key_in = sys.argv[1]
	population_in = int(sys.argv[2])
#
	with grpc.insecure_channel('localhost:50051') as channel:
		stub = redis_update_pb2_grpc.GreeterStub(channel)
		response = stub.RedisUpdate(redis_update_pb2.RedisRequest \
			(key=key_in,population=population_in))
		print("Greeter client received aa: " + response.key)
#
# --------------------------------------------------------------
if __name__ == '__main__':
	logging.basicConfig()
	run()
# --------------------------------------------------------------
```

gRPC のコードを作成します。

```bash
python -m grpc_tools.protoc -I. \
	--python_out=. \
	--grpc_python_out=. \
	./redis_update.proto
```

サーバープログラムの起動

```bash
./redis_update_server.py
```

クライアントプログラムの実行

```text
$ ./redis_update_client.py t0934  9823400
Greeter client received aa: t0934
```