From d1549b3df09a2a60f54e7f986e65be291401a90d Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Thu, 16 Mar 2023 20:22:18 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 27 ++++++++++++++++----------- pkg/utils/context.py | 9 ++++++--- pkg/utils/threadctl.py | 11 +++++++---- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index 131aff75..d58d44c1 100644 --- a/main.py +++ b/main.py @@ -293,14 +293,17 @@ def stop(): if not isinstance(e, KeyboardInterrupt): raise e + # 临时函数,用于加载config和上下文,未来统一放在config类 def load_config(): - #存在性校验 + + # 存在性校验 if not os.path.exists('config.py'): shutil.copy('config-template.py', 'config.py') print('请先在config.py中填写配置') sys.exit(0) - #完整性校验 + + # 完整性校验 is_integrity = True config_template = importlib.import_module('config-template') config = importlib.import_module('config') @@ -313,9 +316,11 @@ def load_config(): logging.warning("配置文件不完整,请依据config-template.py检查config.py") logging.warning("以上配置已被设为默认值,将在5秒后继续启动... ") time.sleep(5) - #context配置 + + # 存进上下文 pkg.utils.context.set_config(config) + def check_file(): # 检查是否有banlist.py,如果没有就把banlist-template.py复制一份 if not os.path.exists('banlist.py'): @@ -335,6 +340,7 @@ def check_file(): if not os.path.exists(path): os.mkdir(path) + def main(): # 加载配置 load_config() @@ -346,13 +352,14 @@ def main(): # 配置线程池 from pkg.utils import ThreadCtl thread_ctl = ThreadCtl( - sys_pool_num = config.sys_pool_num, - admin_pool_num = config.admin_pool_num, - user_pool_num = config.user_pool_num + sys_pool_num=config.sys_pool_num, + admin_pool_num=config.admin_pool_num, + user_pool_num=config.user_pool_num ) + # 存进上下文 pkg.utils.context.set_thread_ctl(thread_ctl) - # 控制台指令处理 + # 启动指令处理 if len(sys.argv) > 1 and sys.argv[1] == 'init_db': init_db() sys.exit(0) @@ -363,10 +370,7 @@ def main(): updater.update_all(cli=True) sys.exit(0) - # 不知道干啥的 - # import pkg.utils.configmgr - # - # pkg.utils.configmgr.set_config_and_reload("quote_origin", False) + # 关闭urllib的http警告 requests.packages.urllib3.disable_warnings(InsecureRequestWarning) pkg.utils.context.get_thread_ctl().submit_sys_task( @@ -388,6 +392,7 @@ def main(): cmd = "kill -9 {}".format(os.getpid()) os.system(cmd) + if __name__ == '__main__': main() diff --git a/pkg/utils/context.py b/pkg/utils/context.py index 854ab085..2f8dee44 100644 --- a/pkg/utils/context.py +++ b/pkg/utils/context.py @@ -1,4 +1,6 @@ import threading +from pkg.utils import ThreadCtl + context = { 'inst': { @@ -78,14 +80,15 @@ def get_plugin_host(): context_lock.release() return t + def set_thread_ctl(inst): context_lock.acquire() context['pool_ctl'] = inst context_lock.release() -from pkg.utils import ThreadCtl + def get_thread_ctl() -> ThreadCtl: context_lock.acquire() - t = context['pool_ctl'] + t: ThreadCtl = context['pool_ctl'] context_lock.release() - return t \ No newline at end of file + return t diff --git a/pkg/utils/threadctl.py b/pkg/utils/threadctl.py index 75a74290..6b70d5f2 100644 --- a/pkg/utils/threadctl.py +++ b/pkg/utils/threadctl.py @@ -1,7 +1,9 @@ -from concurrent.futures import ThreadPoolExecutor, Future -import threading, time +import threading +import time +from concurrent.futures import ThreadPoolExecutor -class Pool(): + +class Pool: ''' 线程池结构 ''' @@ -28,7 +30,8 @@ class Pool(): continue time.sleep(1) -class ThreadCtl(): + +class ThreadCtl: def __init__(self, sys_pool_num, admin_pool_num, user_pool_num): ''' 线程池控制类