diff --git a/paddlehub/common/hub_server.py b/paddlehub/common/hub_server.py index d4d5c0d0205e01eada81ccf51042fd93bb5ee1c0..ba770e28f5411f62a31d1f8657f5e7330b260a1f 100644 --- a/paddlehub/common/hub_server.py +++ b/paddlehub/common/hub_server.py @@ -24,6 +24,7 @@ import requests import json import yaml import random +import threading from random import randint from paddlehub.common import utils, srv_utils @@ -269,6 +270,32 @@ class HubServer(object): print("Request Hub-Server unsuccessfully.") +class CacheUpdater(threading.Thread): + def __init__(self, module, version=None): + threading.Thread.__init__(self) + self.module = module + self.version = version + + def update_resource_list_file(self, module, version=None): + payload = {'word': module} + if version: + payload['version'] = version + api_url = srv_utils.uri_path(default_hub_server.get_server_url(), + 'search') + cache_path = os.path.join(CACHE_HOME, RESOURCE_LIST_FILE) + extra = { + "command": "update_cache", + "mtime": os.stat(cache_path).st_mtime + } + r = srv_utils.hub_request(api_url, payload, extra) + if r.get("update_cache", 0) == 1: + with open(cache_path, 'w+') as fp: + yaml.safe_dump({'resource_list': r['data']}, fp) + + def run(self): + self.update_resource_list_file(self.module, self.version) + + def server_check(): default_hub_server.server_check() diff --git a/paddlehub/module/module.py b/paddlehub/module/module.py index ffd4d673728a0293dc918197f83edbd0ebf753cc..f508403ce9b3aa5d93e51a756a406026c51526d6 100644 --- a/paddlehub/module/module.py +++ b/paddlehub/module/module.py @@ -34,6 +34,7 @@ from paddlehub.common.downloader import default_downloader from paddlehub.module import module_desc_pb2 from paddlehub.common.dir import CONF_HOME from paddlehub.module import check_info_pb2 +from paddlehub.common.hub_server import CacheUpdater from paddlehub.module.signature import Signature, create_signature from paddlehub.module.checker import ModuleChecker from paddlehub.module.manager import default_module_manager @@ -144,6 +145,7 @@ class Module(object): else: lock.flock(fp_lock, lock.LOCK_UN) raise ValueError("Module initialized parameter is empty") + CacheUpdater(name, version).start() def _init_with_name(self, name, version=None): log_msg = "Installing %s module" % name