diff --git a/app.py b/app.py index 6a047a6..8398fdd 100644 --- a/app.py +++ b/app.py @@ -4,6 +4,18 @@ import config from channel import channel_factory from common import log import os +from multiprocessing import Pool + +def startProcess(channel_type): + # 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: @@ -18,13 +30,24 @@ 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) + # 使用主进程启动终端通道 + 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]) - # create channel - channel = channel_factory.create_channel(channel_type) - - # startup channel - channel.startup() + if terminal: + channel = channel_factory.create_channel(terminal) + channel.startup() + # 等待池中所有进程执行完毕 + pool.close() + pool.join() 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 1517abb..be8f3fe 100644 --- a/config-template.json +++ b/config-template.json @@ -17,7 +17,7 @@ } }, "channel": { - "type": "terminal", + "type": ["http","wechat","terminal"], "single_chat_prefix": ["bot", "@bot"], "single_chat_reply_prefix": "[bot] ", "group_chat_prefix": ["@bot"],