From 033a29f6ea8b63213c3dea0302d2875f8be77bb9 Mon Sep 17 00:00:00 2001 From: RA Date: Sun, 19 Mar 2023 00:20:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=BF=9B=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E8=B5=B7=E5=8A=A8=E5=A4=9A=E9=80=9A=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 25 ++++++++++++++++++------- config-template.json | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 4833ed3..bd9ddba 100644 --- a/app.py +++ b/app.py @@ -3,13 +3,18 @@ import config from channel import channel_factory from common import log -from multiprocessing import Process +from multiprocessing import Pool def startProcess(channel_type): - # create channel - channel = channel_factory.create_channel(channel_type) - # startup channel - channel.startup() + # load config + config.load_config() + # create channel + channel = channel_factory.create_channel(channel_type) + # startup channel + channel.startup() + +def wrapper(channel_type): + startProcess(channel_type) if __name__ == '__main__': try: @@ -19,10 +24,16 @@ if __name__ == '__main__': model_type = config.conf().get("model").get("type") channel_type = config.conf().get("channel").get("type") + # 使用进程池启动子进程 + pool = Pool(len(channel_type)) + for type in channel_type: log.info("[INIT] Start up: {} on {}", model_type, type) - p = Process(target=startProcess, args=(type)) - p.start() + pool.apply_async(wrapper, args=[type]) + + # 等待池中所有进程执行完毕 + pool.close() + pool.join() except Exception as e: log.error("App startup failed!") log.exception(e) \ No newline at end of file diff --git a/config-template.json b/config-template.json index 0a22e2e..b18714f 100644 --- a/config-template.json +++ b/config-template.json @@ -15,7 +15,7 @@ }, "channel": { - "type": ["http","terminal","wechat"], + "type": ["http","wechat"], "single_chat_prefix": ["bot", "@bot"], "single_chat_reply_prefix": "[bot] ", "group_chat_prefix": ["@bot"],