diff --git a/paddlehub/commands/serving.py b/paddlehub/commands/serving.py index 81cd75316880d63ce41ac9628e5d52030b19e811..8dced0f13091d4380b274b4a2052cc238e59498c 100644 --- a/paddlehub/commands/serving.py +++ b/paddlehub/commands/serving.py @@ -19,6 +19,7 @@ from __future__ import print_function import argparse import os +import platform import socket import json import paddlehub as hub @@ -39,9 +40,9 @@ class ServingCommand(BaseCommand): usage='%(prog)s', add_help=True) self.parser.add_argument("command") + self.parser.add_argument("sub_command") self.sub_parse = self.parser.add_mutually_exclusive_group( required=False) - self.sub_parse.add_argument("--start", action="store_true") self.parser.add_argument( "--use_gpu", action="store_true", default=False) self.parser.add_argument( @@ -51,7 +52,7 @@ class ServingCommand(BaseCommand): self.parser.add_argument("--port", "-p", nargs="+", default=[8866]) @staticmethod - def port_is_open(ip, port): + def is_port_occupied(ip, port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((ip, int(port))) @@ -93,19 +94,28 @@ class ServingCommand(BaseCommand): @staticmethod def start_serving(args): - if args.use_multiprocess is True: - from paddlehub.serving import app - else: - from paddlehub.serving import app_single as app config_file = args.config if config_file is not None: config_file = config_file[0] if os.path.exists(config_file): with open(config_file, "r") as fp: configs = json.load(fp) + use_multiprocess = configs.get("use_multiprocess", False) + if use_multiprocess is True: + if platform.system() == "Windows": + print( + "Warning: Windows cannot use multiprocess working " + "mode, Hub-Serving will switch to single process mode" + ) + from paddlehub.serving import app_single as app + else: + from paddlehub.serving import app + else: + from paddlehub.serving import app_single as app use_gpu = configs.get("use_gpu", False) port = configs.get("port", 8866) - if ServingCommand.port_is_open("127.0.0.1", port) is True: + if ServingCommand.is_port_occupied("127.0.0.1", + port) is True: print("Port %s is occupied, please change it." % (port)) return False configs = configs.get("modules_info") @@ -120,11 +130,21 @@ class ServingCommand(BaseCommand): else: print("config_file ", config_file, "not exists.") else: + if args.use_multiprocess is True: + if platform.system() == "Windows": + print( + "Warning: Windows cannot use multiprocess working " + "mode, Hub-Serving will switch to single process mode") + from paddlehub.serving import app_single as app + else: + from paddlehub.serving import app + else: + from paddlehub.serving import app_single as app module = args.modules if module is not None: use_gpu = args.use_gpu port = args.port[0] - if ServingCommand.port_is_open("127.0.0.1", port) is True: + if ServingCommand.is_port_occupied("127.0.0.1", port) is True: print("Port %s is occupied, please change it." % (port)) return False module_info = ServingCommand.preinstall_modules(module) @@ -142,9 +162,10 @@ class ServingCommand(BaseCommand): def show_help(): str = "serving