使用进程池起动多通道

This commit is contained in:
RA
2023-03-19 00:20:57 +08:00
parent 1c43111b1b
commit 033a29f6ea
2 changed files with 19 additions and 8 deletions

25
app.py
View File

@@ -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)