From 1c43111b1b1e3b8d56b9a3f167da7b360011058f Mon Sep 17 00:00:00 2001 From: RA Date: Sat, 18 Mar 2023 22:48:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=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 | 20 ++++++++++++-------- config-template.json | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index d555e05..4833ed3 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,13 @@ import config from channel import channel_factory from common import log +from multiprocessing import Process + +def startProcess(channel_type): + # create channel + channel = channel_factory.create_channel(channel_type) + # startup channel + channel.startup() if __name__ == '__main__': try: @@ -12,13 +19,10 @@ if __name__ == '__main__': model_type = config.conf().get("model").get("type") channel_type = config.conf().get("channel").get("type") - log.info("[INIT] Start up: {} on {}", model_type, channel_type) - - # create channel - channel = channel_factory.create_channel(channel_type) - - # startup channel - channel.startup() + for type in channel_type: + log.info("[INIT] Start up: {} on {}", model_type, type) + p = Process(target=startProcess, args=(type)) + p.start() except Exception as e: log.error("App startup failed!") - log.exception(e) + log.exception(e) \ No newline at end of file diff --git a/config-template.json b/config-template.json index 3202a99..0a22e2e 100644 --- a/config-template.json +++ b/config-template.json @@ -15,7 +15,7 @@ }, "channel": { - "type": "terminal", + "type": ["http","terminal","wechat"], "single_chat_prefix": ["bot", "@bot"], "single_chat_reply_prefix": "[bot] ", "group_chat_prefix": ["@bot"], From 033a29f6ea8b63213c3dea0302d2875f8be77bb9 Mon Sep 17 00:00:00 2001 From: RA Date: Sun, 19 Mar 2023 00:20:57 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=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"], From d5944ceb4c438f77a2fca188d8e86962e5ee7d39 Mon Sep 17 00:00:00 2001 From: RA Date: Sun, 19 Mar 2023 00:45:46 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=B0=86=E7=BB=88=E7=AB=AF=E9=80=9A?= =?UTF-8?q?=E9=81=93=E6=94=BE=E5=88=B0=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 12 ++++++++++-- config-template.json | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index bd9ddba..bbfcbb5 100644 --- a/app.py +++ b/app.py @@ -24,13 +24,21 @@ if __name__ == '__main__': model_type = config.conf().get("model").get("type") channel_type = config.conf().get("channel").get("type") - # 使用进程池启动子进程 + # 使用主进程启动终端通道 + if "terminal" in channel_type: + index=channel_type.index("terminal") + terminal = channel_type.pop(index) + else: + terminal = None + # 使用进程池启动其他通道子进程 pool = Pool(len(channel_type)) - for type in channel_type: log.info("[INIT] Start up: {} on {}", model_type, type) pool.apply_async(wrapper, args=[type]) + if terminal: + channel = channel_factory.create_channel(terminal) + channel.startup() # 等待池中所有进程执行完毕 pool.close() pool.join() diff --git a/config-template.json b/config-template.json index b18714f..4ba4094 100644 --- a/config-template.json +++ b/config-template.json @@ -15,7 +15,7 @@ }, "channel": { - "type": ["http","wechat"], + "type": ["http","wechat","terminal"], "single_chat_prefix": ["bot", "@bot"], "single_chat_reply_prefix": "[bot] ", "group_chat_prefix": ["@bot"],