1

我正在尝试按照此示例制作一个 gRPC python 服务器-客户端应用程序,但我无法将服务器放在我的代码上处于侦听状态。添加几乎与示例完全相同的代码并运行该start方法后,指定的端口上没有任何内容正在监听。我的代码是:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import grpc
import interface_pb2
import interface_pb2_grpc
from concurrent import futures
import time
# some other imports...

class GrpcInterface(interface_pb2_grpc.ManipulaMapaServicer):
    def CriaItem(self, request, context):
        # do stuff...

    def LeItem(self, request, context):
        # do stuff...

    def AtualizaItem(self, request, context):
        # do stuff...

    def DeletaItem(self, request, context):
        # do stuff...

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

def main():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    interface_pb2_grpc.add_ManipulaMapaServicer_to_server(GrpcInterface(), server)
    print('Vai iniciar o servidor gRPC na porta ' + str(8888))
    server.add_insecure_port('[::]:' + str(8888))
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print('Erro ao rodar servidor: ')
        print(str(e))

的代码当然是基于我interface_pb2_grpc.ManipulaMapaServicer的自动生成的(使用命令) :python3 -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. interface.protointerface.proto

syntax = "proto3";

message msgItem {
    int64 chave = 1;
    string valor = 2;
}

message status {
    string resposta = 1; 
    msgItem itemResposta = 2; 
}

service ManipulaMapa {
    rpc CriaItem (msgItem) returns (status) {}
    rpc LeItem (msgItem) returns (msgItem) {}
    rpc AtualizaItem (msgItem) returns (status) {}
    rpc DeletaItem (msgItem) returns (status) {}
}

执行到达while True:内部循环,main但端口上没有运行服务器8888。这里有什么问题?顺便说一句,这个问题与这个问题没有重复,因为在最后一个问题中,问题是由在start方法之后运行的垃圾收集器引起的。

4

1 回答 1

0

您可以尝试在调用 main() 之前启用日志记录。如果在 server.start() 调用过程中有任何错误,它将写入错误日志。

也就是说,在 main() 之前添加它。

logging.basicConfig()

例如,如果端口已经被其他进程监听。

E0830 19:35:33.731000000 55600 src/core/ext/transport/chttp2/server/insecure/server_chttp2.cc:40] {"created":"@1567164933.731000000","description":"No address added out of total 1 resolved","file":"src/core/ext/transport/chttp2/server/chttp2_server.cc","file_line":394,"referenced_errors":[{"created":"@1567164933.731000000","description":"Failed to add port to server","file":"src/core/lib/iomgr/tcp_server_windows.cc","file_line":510,"referenced_errors":[{"created":"@1567164933.731000000","description":"OS Error","file":"src/core/lib/iomgr/tcp_server_windows.cc","file_line":201,"os_error":"Only one usage of each socket address (protocol/network address/port) is normally permitted.\r\n","syscall":"bind","wsa_error":10048}]}]}

于 2019-08-30T11:37:45.090 回答