简体   繁体   English

Python gRPC服务器未在指定端口上侦听

[英]Python gRPC server does not listen on specified port

I'm trying to make a gRPC python server-client application following this example but I can't put the server on my code on a listening state. 我正在尝试按照此示例制作gRPC python服务器-客户端应用程序,但无法将服务器置于处于侦听状态的代码中。 After adding the code almost as exactly equals to the example and running the start method yet nothing is listening on the port specified. 在添加几乎与示例完全相同的代码并运行start方法之后,仍然没有任何东西在指定的端口上侦听。 My code is: 我的代码是:

#!/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))

The code for interface_pb2_grpc.ManipulaMapaServicer are of course auto generated (with the command python3 -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. interface.proto ) based on my interface.proto : 当然, interface_pb2_grpc.ManipulaMapaServicer的代码是基于我的interface.proto自动生成的(使用命令python3 -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. 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) {}
}

The execution reaches the while True: loop inside main yet no server running on port 8888 . 执行到达while True:main内部循环,但没有服务器在端口8888上运行。 What can be wrong here? 这里有什么问题? By the way, this question is not duplicate from this one because in this last question the problem was caused by a garbage collector that ran right after the start method. 顺便说一句,这个问题是不是从复制一个,因为在这最后一个问题的问题是由垃圾收集器的后马上跑造成的start方法。

You can try to enable logging before calling main(). 您可以尝试在调用main()之前启用日志记录。 If there is anything wrong during the server.start() calling, it will write error logs. 如果在server.start()调用期间发生任何错误,它将写入错误日志。

That is, add this before main(). 也就是说,将其添加到main()之前。

logging.basicConfig()

For instance, if the port is already listening by other processes. 例如,如果端口已经被其他进程监听。

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}]}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM