首页 > 编程语言 >python的优雅退出

python的优雅退出

时间:2022-10-26 16:11:07浏览次数:59  
标签:__ python signal server grpc user 退出 import 优雅

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import signal
import sys
from concurrent import futures
import logging
from loguru import logger
import grpc

from user_srv.handler.user import UserServicer
from user_srv.proto import user_pb2_grpc


BASE_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))


def on_exit(signo, frame):
    logger.info("进程中断")
    sys.exit(0)


def serve():
    logger.add("logs/user_srv_{time}.log")
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    user_pb2_grpc.add_UserServicer_to_server(UserServicer(), server)
    server.add_insecure_port('[::]:50051')

    # 主进程信号退出
    """
        WINDOWS下支持的信号是有限的
            SIGINT ctrl + c终端
            SIGTERM KILL发出的软件终止
    """
    signal.signal(signal.SIGINT, on_exit)
    signal.signal(signal.SIGTERM, on_exit)

    logger.info("启动服务:127.0.0.1::50051")
    server.start()
    server.wait_for_termination()


if __name__ == '__main__':
    logging.basicConfig()
    serve()

  

标签:__,python,signal,server,grpc,user,退出,import,优雅
From: https://www.cnblogs.com/wlike/p/16828774.html

相关文章