gpt4 book ai didi

Python gRPC 服务器不监听指定端口

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:24 27 4
gpt4 key购买 nike

我正在尝试按照 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) {}
}

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

最佳答案

您可以尝试在调用 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}]}]}

关于Python gRPC 服务器不监听指定端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52998698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com