util.py 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# coding:utf-8
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import signal
import os
import time
import json
from paddle_serving_server.env import CONF_HOME


def pid_is_exist(pid: int):
    '''
    Try to kill process by PID.

    Args:
        pid(int): PID of process to be killed.

    Returns:
         True if PID will be killed.

    Examples:
    .. code-block:: python

        pid_is_exist(pid=8866)
    '''
    try:
        os.kill(pid, 0)
    except:
        return False
    else:
        return True

def kill_stop_process_by_pid(command : str, pid : int):
    '''
    using different signals to kill process group by PID .

    Args:
        command(str): stop->SIGINT, kill->SIGKILL
        pid(int): PID of process to be killed.

    Returns:
       None
    Examples:
    .. code-block:: python

       kill_stop_process_by_pid("stop", 9494)
    '''
    if not pid_is_exist(pid):
        print("Process [%s]  has been stopped."%pid)
        return
62 63 64 65 66 67 68 69 70 71 72 73 74
    if platform.system() == "Windows":
        os.kill(pid, signal.SIGINT)   
    else:
        try:
             if command == "stop":
                 os.killpg(pid, signal.SIGINT)
             elif command == "kill":
                 os.killpg(pid, signal.SIGKILL)
        except ProcessLookupError:
             if command == "stop":
                 os.kill(pid, signal.SIGINT)
             elif command == "kill":
                 os.kill(pid, signal.SIGKILL)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

def dump_pid_file(portList, model):
    '''
    Write PID info to file.

    Args:
        portList(List): PiplineServing includes http_port and rpc_port
                        PaddleServing include one port
        model(str): 'Pipline' for PiplineServing
                    Specific model list for ServingModel 

    Returns:
       None
    Examples:
    .. code-block:: python

       dump_pid_file([9494, 10082], 'serve')
    '''
    pid = os.getpid()
94
    gid = os.getpgid(pid)
95 96 97 98 99 100 101 102 103 104 105 106 107 108
    pidInfoList = []
    filepath = os.path.join(CONF_HOME, "ProcessInfo.json")
    if os.path.exists(filepath):
        if os.path.getsize(filepath):
            with open(filepath, "r") as fp:
                pidInfoList = json.load(fp)
                # delete old pid data when new port number is same as old's
                for info in pidInfoList:
                    storedPort = list(info["port"])
                    interList = list(set(portList)&set(storedPort))
                    if interList:
                        pidInfoList.remove(info)

    with open(filepath, "w") as fp:
109
        info ={"pid": gid, "port" : portList, "model" : str(model), "start_time" : time.time()}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        pidInfoList.append(info)
        json.dump(pidInfoList, fp)

def load_pid_file(filepath: str):
    '''
    Read PID info from file.
    '''
    if not os.path.exists(filepath):
        raise ValueError(
            "ProcessInfo.json file is not exists, All processes of PaddleServing has been stopped.")
        return False
    
    if os.path.getsize(filepath):
        with open(filepath, "r") as fp:
            infoList = json.load(fp)
            return infoList
    else:
        os.remove(filepath)
        print("ProcessInfo.json file is empty, All processes of PaddleServing has been stopped.")
        return False