我正在尝试在this example之后创建一个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.proto
自动生成的(使用命令interface.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
上运行。这里有什么问题?顺便说一句,这个问题与this并没有重复,因为在最后一个问题中,问题是由在start
方法之后运行的垃圾收集器引起的。
答案 0 :(得分: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}]}]}