From 3802c07c5a76a7c6ee584776818ec2db283022eb Mon Sep 17 00:00:00 2001 From: ShenYuhan Date: Tue, 7 Jan 2020 19:22:35 +0800 Subject: [PATCH] Add hub_name --- paddlehub/commands/config.py | 2 -- paddlehub/commands/serving.py | 5 +++-- paddlehub/common/hub_server.py | 2 +- paddlehub/common/server_config.py | 7 +++++-- paddlehub/common/srv_utils.py | 28 ++++++++++++++++++++++++++++ paddlehub/common/utils.py | 22 ---------------------- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/paddlehub/commands/config.py b/paddlehub/commands/config.py index 265fcd8d..54994c9b 100644 --- a/paddlehub/commands/config.py +++ b/paddlehub/commands/config.py @@ -25,7 +25,6 @@ import re from paddlehub.commands.base_command import BaseCommand, ENTRY from paddlehub.common.dir import CONF_HOME from paddlehub.common.server_config import default_server_config -from paddlehub.common.hub_server import CacheUpdater from paddlehub.common.hub_server import HubServer HubServer() @@ -102,7 +101,6 @@ class ConfigCommand(BaseCommand): print(str) def execute(self, argv): - CacheUpdater("hub_config").start() args = self.parser.parse_args() if args.option is None: ConfigCommand.show_config() diff --git a/paddlehub/commands/serving.py b/paddlehub/commands/serving.py index 2c221e01..7707a900 100644 --- a/paddlehub/commands/serving.py +++ b/paddlehub/commands/serving.py @@ -239,8 +239,7 @@ class ServingCommand(BaseCommand): StandaloneApplication( app.create_app(init_flag=False, configs=configs), options).run() - @staticmethod - def start_single_app_with_file(configs): + def start_single_app_with_file(self, configs): use_gpu = configs.get("use_gpu", False) port = configs.get("port", 8866) if ServingCommand.is_port_occupied("127.0.0.1", port) is True: @@ -251,6 +250,7 @@ class ServingCommand(BaseCommand): module_info = ServingCommand.preinstall_modules(module) for index in range(len(module_info)): configs[index].update(module_info[index]) + self.dump_pid_file() app.run(use_gpu, configs=configs, port=port) @staticmethod @@ -304,6 +304,7 @@ class ServingCommand(BaseCommand): "queue_size": 20 }) for item in module_info ] + self.dump_pid_file() app.run(use_gpu, configs=module_info, port=port) else: print("Lack of necessary parameters!") diff --git a/paddlehub/common/hub_server.py b/paddlehub/common/hub_server.py index 07123cdc..b73be696 100644 --- a/paddlehub/common/hub_server.py +++ b/paddlehub/common/hub_server.py @@ -33,7 +33,7 @@ from paddlehub.common.server_config import default_server_config from paddlehub.io.parser import yaml_parser from paddlehub.common.lock import lock from paddlehub.common.dir import CONF_HOME, CACHE_HOME -from paddlehub.common.utils import ConfigInfo +from paddlehub.common.srv_utils import ConfigInfo RESOURCE_LIST_FILE = "resource_list_file.yml" CACHE_TIME = 60 * 10 diff --git a/paddlehub/common/server_config.py b/paddlehub/common/server_config.py index 72d18f0c..4105740c 100644 --- a/paddlehub/common/server_config.py +++ b/paddlehub/common/server_config.py @@ -11,14 +11,17 @@ # 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. -from paddlehub.common.utils import ConfigInfo +import time +import uuid +from paddlehub.common.utils import md5 HUB_SERVERS = ["http://paddlepaddle.org.cn/paddlehub"] +hub_name = md5(str(uuid.uuid1())[-12:]) + "-" + str(int(time.time())) default_server_config = { "server_url": HUB_SERVERS, "resource_storage_server_url": "https://bj.bcebos.com/paddlehub-data/", "debug": False, "log_level": "DEBUG", - "hub_name": ConfigInfo().get_hub_name() + "hub_name": hub_name } diff --git a/paddlehub/common/srv_utils.py b/paddlehub/common/srv_utils.py index 7c189990..1181774c 100644 --- a/paddlehub/common/srv_utils.py +++ b/paddlehub/common/srv_utils.py @@ -15,8 +15,15 @@ import requests import paddle import json +import time +import uuid +import os from paddlehub import version +from paddlehub.common.dir import CONF_HOME +from paddlehub.common.decorator_utils import singleton +from paddlehub.common.utils import md5 +from paddlehub.common.server_config import default_server_config def uri_path(server_url, api): @@ -37,3 +44,24 @@ def hub_request(api, params, extra=None, timeout=8): params["extra"] = json.dumps(extra) r = requests.get(api, params, timeout=timeout) return r.json() + + +@singleton +class ConfigInfo(object): + def __init__(self): + self.filepath = os.path.join(CONF_HOME, "config.json") + self.hub_name = None + self.configs = None + if os.path.exists(self.filepath): + with open(self.filepath, "r") as fp: + self.configs = json.load(fp) + self.hub_name = self.configs.get("hub_name", None) + + def get_hub_name(self): + if self.hub_name is None: + self.hub_name = md5(str(uuid.uuid1())[-12:]) + "-" + str( + int(time.time())) + with open(self.filepath, "w") as fp: + fp.write(json.dumps(default_server_config)) + + return self.hub_name diff --git a/paddlehub/common/utils.py b/paddlehub/common/utils.py index bab476cd..45c02a42 100644 --- a/paddlehub/common/utils.py +++ b/paddlehub/common/utils.py @@ -22,16 +22,12 @@ import os import multiprocessing import hashlib import platform -import uuid -import json import paddle.fluid as fluid import six from paddlehub.module import module_desc_pb2 from paddlehub.common.logger import logger -from paddlehub.common.dir import CONF_HOME -from paddlehub.common.decorator_utils import singleton def version_compare(version1, version2): @@ -59,24 +55,6 @@ def get_platform(): return platform.platform() -@singleton -class ConfigInfo(object): - def __init__(self): - self.filepath = os.path.join(CONF_HOME, "config.json") - self.hub_name = None - self.configs = None - if os.path.exists(self.filepath): - with open(self.filepath, "r") as fp: - self.configs = json.load(fp) - self.use_id = self.configs.get("hub_name", None) - - def get_hub_name(self): - if self.hub_name is None: - hub_name = uuid.UUID(int=uuid.getnode()).hex[-12:] - self.hub_name = md5(hub_name) - return self.hub_name - - def is_windows(): return get_platform().lower().startswith("windows") -- GitLab