使用Python的gRPC创建Redis数据

需要三个东西:设置文件、服务器程序和客户端程序。

设定文件 (shè


syntax = "proto3";

package redis_create;

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

message RedisRequest {
    string key = 1;
    string str_json = 2;
}

message RedisReply {
    string key = 1;
}

服务器程序

#! /usr/bin/python
#
#   redis_create_server.py
#
#                   Feb/08/2020
# --------------------------------------------------------------
import sys
import redis
from concurrent import futures
import logging

import grpc

import redis_create_pb2
import redis_create_pb2_grpc

# --------------------------------------------------------------
def redis_create_proc(key_in,str_json):
    rr = redis.Redis(host='localhost', port=6379, db=0)
#
    rr.set(key_in, str_json)
#
    return str_json
# --------------------------------------------------------------
class Greeter(redis_create_pb2_grpc.GreeterServicer):

    def RedisCreate(self, request, context):
        sys.stderr.write("*** RedisRead ***\n")
        sys.stderr.write("*** key = %s ***\n" % request.key)
        sys.stderr.write("*** str_json = %s ***\n" % request.str_json)
        redis_create_proc(request.key,request.str_json)
        return redis_create_pb2.RedisReply(key=request.key)

# --------------------------------------------------------------
def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    redis_create_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()
# --------------------------------------------------------------

客户端程序

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

import redis_create_pb2
import redis_create_pb2_grpc

# --------------------------------------------------------------
def run():
    unit_aa = {}
    key_in = sys.argv[1]
    unit_aa['name'] = sys.argv[2]
    unit_aa['population'] = sys.argv[3]
    unit_aa['date_mod'] = sys.argv[4]
#
    str_json_in = json.dumps(unit_aa)
#
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = redis_create_pb2_grpc.GreeterStub(channel)
        response = stub.RedisCreate(redis_create_pb2.RedisRequest \
            (key=key_in,str_json = str_json_in))
        print("Greeter client received aa: " + response.key)
#
# --------------------------------------------------------------
if __name__ == '__main__':
    logging.basicConfig()
    run()
# --------------------------------------------------------------

我们将创建 gRPC 的代码。

python -m grpc_tools.protoc -I. \
    --python_out=. \
    --grpc_python_out=. \
    ./redis_create.proto

启动服务器程序

./redis_create_server.py

客户端程序的执行

$ ./redis_create_client.py t0931 那須烏山 42935 2003-8-9
Greeter client received aa: t0931
广告
将在 10 秒后关闭
bannerAds