未验证 提交 d78604b5 编写于 作者: 走神的阿圆's avatar 走神的阿圆 提交者: GitHub

update cache info if necessary

上级 35b00882
...@@ -22,6 +22,7 @@ from string import Template ...@@ -22,6 +22,7 @@ from string import Template
from paddlehub.env import TMP_HOME as tmp_dir from paddlehub.env import TMP_HOME as tmp_dir
from paddlehub.commands import register from paddlehub.commands import register
from paddlehub.utils.xarfile import XarFile from paddlehub.utils.xarfile import XarFile
from paddlehub.server.server import CacheUpdater
INIT_FILE = '__init__.py' INIT_FILE = '__init__.py'
MODULE_FILE = 'module.py' MODULE_FILE = 'module.py'
...@@ -113,6 +114,7 @@ class ConvertCommand: ...@@ -113,6 +114,7 @@ class ConvertCommand:
return False return False
self.module = args.module_name self.module = args.module_name
self.version = args.module_version if args.module_version is not None else '1.0.0' self.version = args.module_version if args.module_version is not None else '1.0.0'
CacheUpdater("hub_convert", module=self.module, version=self.version).start()
self.src = args.model_dir self.src = args.model_dir
if not os.path.isdir(self.src): if not os.path.isdir(self.src):
print('`{}` is not exists or not a directory path'.format(self.src)) print('`{}` is not exists or not a directory path'.format(self.src))
......
...@@ -19,6 +19,7 @@ import paddlehub as hub ...@@ -19,6 +19,7 @@ import paddlehub as hub
from paddlehub.commands import register from paddlehub.commands import register
from paddlehub.server import module_server from paddlehub.server import module_server
from paddlehub.utils import utils, log from paddlehub.utils import utils, log
from paddlehub.server.server import CacheUpdater
@register(name='hub.download', description='Download PaddlePaddle pretrained module files.') @register(name='hub.download', description='Download PaddlePaddle pretrained module files.')
...@@ -30,6 +31,7 @@ class DownloadCommand: ...@@ -30,6 +31,7 @@ class DownloadCommand:
for _arg in argv: for _arg in argv:
result = module_server.search_module(_arg) result = module_server.search_module(_arg)
CacheUpdater("hub_download", _arg).start()
if result: if result:
url = result[0]['url'] url = result[0]['url']
with log.ProgressBar('Download {}'.format(url)) as bar: with log.ProgressBar('Download {}'.format(url)) as bar:
......
...@@ -20,6 +20,7 @@ from typing import List ...@@ -20,6 +20,7 @@ from typing import List
from paddlehub.commands import register from paddlehub.commands import register
from paddlehub.module.manager import LocalModuleManager from paddlehub.module.manager import LocalModuleManager
from paddlehub.utils import xarfile from paddlehub.utils import xarfile
from paddlehub.server.server import CacheUpdater
@register(name='hub.install', description='Install PaddleHub module.') @register(name='hub.install', description='Install PaddleHub module.')
...@@ -39,5 +40,6 @@ class InstallCommand: ...@@ -39,5 +40,6 @@ class InstallCommand:
_arg = _arg.split('==') _arg = _arg.split('==')
name = _arg[0] name = _arg[0]
version = None if len(_arg) == 1 else _arg[1] version = None if len(_arg) == 1 else _arg[1]
CacheUpdater("hub_install", name, version).start()
manager.install(name=name, version=version) manager.install(name=name, version=version)
return True return True
...@@ -22,6 +22,7 @@ from paddlehub.compat.module.module_v1 import ModuleV1 ...@@ -22,6 +22,7 @@ from paddlehub.compat.module.module_v1 import ModuleV1
from paddlehub.commands import register from paddlehub.commands import register
from paddlehub.module.manager import LocalModuleManager from paddlehub.module.manager import LocalModuleManager
from paddlehub.module.module import Module, InvalidHubModule from paddlehub.module.module import Module, InvalidHubModule
from paddlehub.server.server import CacheUpdater
@register(name='hub.run', description='Run the specific module.') @register(name='hub.run', description='Run the specific module.')
...@@ -31,6 +32,7 @@ class RunCommand: ...@@ -31,6 +32,7 @@ class RunCommand:
print('ERROR: You must give one module to run.') print('ERROR: You must give one module to run.')
return False return False
module_name = argv[0] module_name = argv[0]
CacheUpdater("hub_run", module_name).start()
if os.path.exists(module_name) and os.path.isdir(module_name): if os.path.exists(module_name) and os.path.isdir(module_name):
try: try:
......
...@@ -21,6 +21,7 @@ from paddlehub.commands import register ...@@ -21,6 +21,7 @@ from paddlehub.commands import register
from paddlehub.module.manager import LocalModuleManager from paddlehub.module.manager import LocalModuleManager
from paddlehub.server.server import module_server from paddlehub.server.server import module_server
from paddlehub.utils import log, platform from paddlehub.utils import log, platform
from paddlehub.server.server import CacheUpdater
@register(name='hub.search', description='Search PaddleHub pretrained model through model keywords.') @register(name='hub.search', description='Search PaddleHub pretrained model through model keywords.')
...@@ -31,8 +32,9 @@ class SearchCommand: ...@@ -31,8 +32,9 @@ class SearchCommand:
widths = [20, 8, 30] if platform.is_windows() else [30, 8, 40] widths = [20, 8, 30] if platform.is_windows() else [30, 8, 40]
table = log.Table(widths=widths) table = log.Table(widths=widths)
table.append(*['ModuleName', 'Version', 'Summary'], aligns=['^', '^', '^'], colors=["blue", "blue", "blue"]) table.append(*['ModuleName', 'Version', 'Summary'], aligns=['^', '^', '^'], colors=["blue", "blue", "blue"])
CacheUpdater("hub_search", argv).start()
results = module_server.search_module(name=argv) results = module_server.search_module(name=argv)
for result in results: for result in results:
table.append(result['name'], result['version'], result['summary']) table.append(result['name'], result['version'], result['summary'])
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
import argparse import argparse
import os import os
import platform import platform
import socket
import json import json
import multiprocessing import multiprocessing
import time import time
...@@ -29,6 +28,7 @@ from paddlehub.env import CONF_HOME ...@@ -29,6 +28,7 @@ from paddlehub.env import CONF_HOME
from paddlehub.serving.http_server import run_all, StandaloneApplication from paddlehub.serving.http_server import run_all, StandaloneApplication
from paddlehub.utils import log from paddlehub.utils import log
from paddlehub.utils.utils import is_port_occupied from paddlehub.utils.utils import is_port_occupied
from paddlehub.server.server import CacheUpdater
def number_of_workers(): def number_of_workers():
...@@ -103,6 +103,9 @@ class ServingCommand: ...@@ -103,6 +103,9 @@ class ServingCommand:
if info is False: if info is False:
return return
pid = info["pid"] pid = info["pid"]
module = info["module"]
start_time = info["start_time"]
CacheUpdater("hub_serving_stop", module=module, addition={"period_time": time.time() - start_time}).start()
if os.path.exists(filepath): if os.path.exists(filepath):
os.remove(filepath) os.remove(filepath)
...@@ -110,7 +113,6 @@ class ServingCommand: ...@@ -110,7 +113,6 @@ class ServingCommand:
log.logger.info("PaddleHub Serving has been stopped.") log.logger.info("PaddleHub Serving has been stopped.")
return return
log.logger.info("PaddleHub Serving will stop.") log.logger.info("PaddleHub Serving will stop.")
# CacheUpdater("hub_serving_stop", module=module, addition={"period_time": time.time() - start_time}).start()
if platform.system() == "Windows": if platform.system() == "Windows":
os.kill(pid, signal.SIGTERM) os.kill(pid, signal.SIGTERM)
else: else:
...@@ -132,7 +134,7 @@ class ServingCommand: ...@@ -132,7 +134,7 @@ class ServingCommand:
from paddle_gpu_serving.run import BertServer from paddle_gpu_serving.run import BertServer
bs = BertServer(with_gpu=args.use_gpu) bs = BertServer(with_gpu=args.use_gpu)
bs.with_model(model_name=args.modules[0]) bs.with_model(model_name=args.modules[0])
# CacheUpdater("hub_bert_service", module=args.modules[0], version="0.0.0").start() CacheUpdater("hub_bert_service", module=args.modules[0], version="0.0.0").start()
bs.run(gpu_index=args.gpu, port=int(args.port)) bs.run(gpu_index=args.gpu, port=int(args.port))
def preinstall_modules(self): def preinstall_modules(self):
...@@ -141,8 +143,7 @@ class ServingCommand: ...@@ -141,8 +143,7 @@ class ServingCommand:
''' '''
for key, value in self.modules_info.items(): for key, value in self.modules_info.items():
init_args = value["init_args"] init_args = value["init_args"]
# CacheUpdater("hub_serving_start", module=key, version=init_args.get("version", "0.0.0")).start() CacheUpdater("hub_serving_start", module=key, version=init_args.get("version", "0.0.0")).start()
if "directory" not in init_args: if "directory" not in init_args:
init_args.update({"name": key}) init_args.update({"name": key})
m = hub.Module(**init_args) m = hub.Module(**init_args)
...@@ -183,19 +184,22 @@ class ServingCommand: ...@@ -183,19 +184,22 @@ class ServingCommand:
Start one PaddleHub-Serving instance by arguments with zmq. Start one PaddleHub-Serving instance by arguments with zmq.
''' '''
if self.modules_info is not None: if self.modules_info is not None:
for module, info in self.modules_info.items():
CacheUpdater("hub_serving_start", module=module, version=info['init_args']['version']).start()
front_port = self.args.port front_port = self.args.port
if is_port_occupied("127.0.0.1", front_port) is True: if is_port_occupied("127.0.0.1", front_port) is True:
log.logger.error("Port %s is occupied, please change it." % front_port) log.logger.error("Port %s is occupied, please change it." % front_port)
return False return False
back_port = int(front_port) + 1 back_port = int(front_port) + 1
for index in range(100): for index in range(100):
if is_port_occupied("127.0.0.1", back_port): if not is_port_occupied("127.0.0.1", back_port):
break break
else: else:
back_port = int(back_port) + 1 back_port = int(back_port) + 1
else: else:
raise RuntimeError("Port from %s to %s is occupied, please use another port" % int(front_port) + 1, raise RuntimeError(
back_port) "Port from %s to %s is occupied, please use another port" % (int(front_port) + 1, back_port))
self.dump_pid_file()
run_all(self.modules_info, self.args.gpu, front_port, back_port) run_all(self.modules_info, self.args.gpu, front_port, back_port)
else: else:
......
...@@ -13,15 +13,23 @@ ...@@ -13,15 +13,23 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import hashlib
import os import os
import time
import json import json
import uuid
import yaml import yaml
from easydict import EasyDict from easydict import EasyDict
import paddlehub.env as hubenv import paddlehub.env as hubenv
def md5(text: str):
'''Calculate the md5 value of the input text.'''
md5code = hashlib.md5(text.encode())
return md5code.hexdigest()
class HubConfig: class HubConfig:
''' '''
PaddleHub configuration management class. Each time the PaddleHub package is loaded, PaddleHub will set the PaddleHub configuration management class. Each time the PaddleHub package is loaded, PaddleHub will set the
...@@ -106,6 +114,48 @@ class HubConfig: ...@@ -106,6 +114,48 @@ class HubConfig:
return yaml.dump(cfg) return yaml.dump(cfg)
class CacheConfig(object):
def __init__(self):
self._initialize()
self.file = os.path.join(hubenv.CONF_HOME, 'cache.yaml')
if not os.path.exists(self.file):
self.flush()
return
with open(self.file, 'r') as file:
try:
cfg = yaml.load(file, Loader=yaml.FullLoader)
self.data.update(cfg)
except:
...
def _initialize(self):
# Set default configuration values.
self.data = EasyDict()
hub_name = md5(str(uuid.uuid1())[-12:]) + "-" + str(int(time.time()))
self.data.hub_name = hub_name
@property
def hub_name(self):
return self.data.hub_name
@hub_name.setter
def hub_name(self, url: str):
self.data.server = url
self.flush()
def flush(self):
'''Flush the current configuration into the configuration file.'''
with open(self.file, 'w') as file:
# convert EasyDict to dict
cfg = json.loads(json.dumps(self.data))
yaml.dump(cfg, file)
def __str__(self):
cfg = json.loads(json.dumps(self.data))
return yaml.dump(cfg)
def _load_old_config(config: HubConfig): def _load_old_config(config: HubConfig):
# The old version of the configuration file is obsolete, read the configuration value and delete it. # The old version of the configuration file is obsolete, read the configuration value and delete it.
old_cfg_file = os.path.join(hubenv.CONF_HOME, 'config.json') old_cfg_file = os.path.join(hubenv.CONF_HOME, 'config.json')
...@@ -122,3 +172,4 @@ def _load_old_config(config: HubConfig): ...@@ -122,3 +172,4 @@ def _load_old_config(config: HubConfig):
config = HubConfig() config = HubConfig()
_load_old_config(config) _load_old_config(config)
cache_config = CacheConfig()
...@@ -159,12 +159,15 @@ class Module(object): ...@@ -159,12 +159,15 @@ class Module(object):
branch: str = None, branch: str = None,
**kwargs): **kwargs):
if cls.__name__ == 'Module': if cls.__name__ == 'Module':
from paddlehub.server.server import CacheUpdater
# This branch come from hub.Module(name='xxx') or hub.Module(directory='xxx') # This branch come from hub.Module(name='xxx') or hub.Module(directory='xxx')
if name: if name:
module = cls.init_with_name( module = cls.init_with_name(
name=name, version=version, source=source, update=update, branch=branch, **kwargs) name=name, version=version, source=source, update=update, branch=branch, **kwargs)
CacheUpdater("update_cache", module=name, version=version).start()
elif directory: elif directory:
module = cls.init_with_directory(directory=directory, **kwargs) module = cls.init_with_directory(directory=directory, **kwargs)
CacheUpdater("update_cache", module=directory, version="0.0.0").start()
else: else:
module = object.__new__(cls) module = object.__new__(cls)
......
...@@ -13,10 +13,20 @@ ...@@ -13,10 +13,20 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import os
import requests
import threading
import time
import yaml
from collections import OrderedDict from collections import OrderedDict
from typing import List from typing import List
import paddle
import paddlehub
import paddlehub.config as hubconf import paddlehub.config as hubconf
from paddlehub.config import cache_config
from paddlehub.server import ServerSource, GitSource from paddlehub.server import ServerSource, GitSource
from paddlehub.utils import utils from paddlehub.utils import utils
...@@ -115,5 +125,63 @@ class HubServer(object): ...@@ -115,5 +125,63 @@ class HubServer(object):
return {} return {}
def uri_path(server_url, api):
srv = server_url
if server_url.endswith('/'):
srv = server_url[:-1]
if api.startswith('/'):
srv += api
else:
api = '/' + api
srv += api
return srv
def hub_request(api, params, extra=None, timeout=8):
params['hub_version'] = paddlehub.__version__.split('-')[0]
params['paddle_version'] = paddle.__version__.split('-')[0]
params["extra"] = json.dumps(extra)
r = requests.get(api, params, timeout=timeout)
return r.json()
class CacheUpdater(threading.Thread):
def __init__(self, command="update_cache", module=None, version=None, addition=None):
threading.Thread.__init__(self)
self.command = command
self.module = module
self.version = version
self.addition = addition
def update_resource_list_file(self, command="update_cache", module=None, version=None, addition=None):
payload = {'word': module}
if version:
payload['version'] = version
api_url = uri_path(hubconf.server, 'search')
cache_path = os.path.join("~")
hub_name = cache_config.hub_name
if os.path.exists(cache_path):
extra = {"command": command, "mtime": os.stat(cache_path).st_mtime, "hub_name": hub_name}
else:
extra = {
"command": command,
"mtime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
"hub_name": hub_name
}
if addition is not None:
extra.update({"addition": addition})
try:
r = hub_request(api_url, payload, extra, timeout=1)
if r.get("update_cache", 0) == 1:
with open(cache_path, 'w+') as fp:
yaml.safe_dump({'resource_list': r['data']}, fp)
except Exception as err:
pass
def run(self):
self.update_resource_list_file(self.command, self.module, self.version, self.addition)
module_server = HubServer() module_server = HubServer()
module_server.add_source(hubconf.server, source_type='server', key='default_hub_server') module_server.add_source(hubconf.server, source_type='server', key='default_hub_server')
...@@ -99,12 +99,13 @@ class InferenceServer(object): ...@@ -99,12 +99,13 @@ class InferenceServer(object):
if platform.system() == "Windows": if platform.system() == "Windows":
back_port = int(port) + 1 back_port = int(port) + 1
for index in range(100): for index in range(100):
if is_port_occupied("127.0.0.1", back_port): if not is_port_occupied("127.0.0.1", back_port):
break break
else: else:
back_port = int(back_port) + 1 back_port = int(back_port) + 1
else: else:
raise RuntimeError("Port from %s to %s is occupied, please use another port" % int(port) + 1, back_port) raise RuntimeError(
"Port from %s to %s is occupied, please use another port" % (int(port) + 1, back_port))
worker_backend = "tcp://localhost:%s" % back_port worker_backend = "tcp://localhost:%s" % back_port
backend = "tcp://*:%s" % back_port backend = "tcp://*:%s" % back_port
else: else:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册