Skip to content

Commit

Permalink
新增 联动程序板块,支持在运行webui的时候协同运行其他程序,省得点来点去;违禁词删减部分
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Mar 26, 2024
1 parent 52a0bcd commit bae0de4
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 21 deletions.
17 changes: 16 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,20 @@
}
]
},
"coordination_program": [
{
"enable": false,
"name": "captions_printer",
"executable": "E://GitHub_pro//captions_printer//pkg//captions_printer-v4.1//Miniconda3//python.exe",
"parameters": ["E://GitHub_pro//captions_printer//pkg//captions_printer-v4.1//app.py"]
},
{
"enable": false,
"name": "audio_player",
"executable": "E://GitHub_pro//audio_player//pkg//audio_player_v2-20240320//Miniconda3//python.exe",
"parameters": ["E://GitHub_pro//audio_player//pkg//audio_player_v2-20240320//app.py"]
}
],
"assistant_anchor": {
"enable": false,
"username": "助播",
Expand Down Expand Up @@ -1527,7 +1541,8 @@
"key_mapping": true,
"custom_cmd": true,
"trends_config": true,
"abnormal_alarm": true
"abnormal_alarm": true,
"coordination_program": true
},
"llm": {
"chatgpt": true,
Expand Down
17 changes: 16 additions & 1 deletion config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,20 @@
}
]
},
"coordination_program": [
{
"enable": false,
"name": "captions_printer",
"executable": "E://GitHub_pro//captions_printer//pkg//captions_printer-v4.1//Miniconda3//python.exe",
"parameters": ["E://GitHub_pro//captions_printer//pkg//captions_printer-v4.1//app.py"]
},
{
"enable": false,
"name": "audio_player",
"executable": "E://GitHub_pro//audio_player//pkg//audio_player_v2-20240320//Miniconda3//python.exe",
"parameters": ["E://GitHub_pro//audio_player//pkg//audio_player_v2-20240320//app.py"]
}
],
"assistant_anchor": {
"enable": false,
"username": "助播",
Expand Down Expand Up @@ -1527,7 +1541,8 @@
"key_mapping": true,
"custom_cmd": true,
"trends_config": true,
"abnormal_alarm": true
"abnormal_alarm": true,
"coordination_program": true
},
"llm": {
"chatgpt": true,
Expand Down
7 changes: 0 additions & 7 deletions data/badwords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,6 @@ bili_
陆委会
神婆
战牌
复制
垡囵
朱嘉明
慰春情
Expand All @@ -1840,7 +1839,6 @@ bili_
彭枫
赛你娘
機八
透视
杜智富
毛你姥
捅他
Expand Down Expand Up @@ -2042,7 +2040,6 @@ bili_
气枪弹
操肏
陈建国
看扣
指导员
张建平
她媽地
Expand All @@ -2069,7 +2066,6 @@ bili_
姬胜德
护士穴
上访
姜洪禄
地西泮
陰戶
Expand All @@ -2078,7 +2074,6 @@ bili_
劳改
骚浪
希葬
点金
车臣
勃起
盜撮
Expand Down Expand Up @@ -2139,7 +2134,6 @@ bili_
长刀
喷射
刘正荣
白丝
卖言正
嫩逼
金伯帆
Expand Down Expand Up @@ -2176,7 +2170,6 @@ bili_
武雷
气木
通告
搏客
抿主
方毅
Expand Down
85 changes: 85 additions & 0 deletions tests/test_common/subprocess1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import subprocess
import json
import os, time, logging
import signal

config_json = '''
{
"coordination_program": [
{
"name": "captions_printer",
"path": "E://GitHub_pro//captions_printer//pkg//captions_printer-v4.1//Miniconda3//python.exe",
"parameters": ["E://GitHub_pro//captions_printer//pkg//captions_printer-v4.1//app.py"]
},
{
"name": "audio_player",
"path": "E://GitHub_pro//audio_player//pkg//audio_player_v2-20240320//Miniconda3//python.exe",
"parameters": ["E://GitHub_pro//audio_player//pkg//audio_player_v2-20240320//app.py"]
}
]
}
'''

# 解析 JSON 配置
config = json.loads(config_json)

# 存储启动的进程
processes = {}

def start_programs(config):
"""根据配置启动所有程序。
Args:
config (dict): 包含程序配置的字典。
"""
for program in config.get("programs", []):
name = program["name"]
python_path = program["path"] # Python 解释器的路径
app_path = program["parameters"][0] # 假设第一个参数总是 app.py 的路径

# 从 app.py 的路径中提取目录
app_dir = os.path.dirname(app_path)

# 使用 Python 解释器路径和 app.py 路径构建命令
cmd = [python_path, app_path]

logging.info(f"运行程序: {name} 位于: {app_dir}")

# 在 app.py 文件所在的目录中启动程序
process = subprocess.Popen(cmd, cwd=app_dir, shell=True)
processes[name] = process

def stop_program(name):
"""停止一个正在运行的程序及其所有子进程,兼容 Windows、Linux 和 macOS。
Args:
name (str): 要停止的程序的名称。
"""
if name in processes:
pid = processes[name].pid # 获取进程ID
logging.info(f"停止程序和它所有的子进程: {name} with PID {pid}")

try:
if os.name == 'nt': # Windows
command = ["taskkill", "/F", "/T", "/PID", str(pid)]
subprocess.run(command, check=True)
else: # POSIX系统,如Linux和macOS
os.killpg(os.getpgid(pid), signal.SIGKILL)

logging.info(f"程序 {name} 和 它所有的子进程都被终止.")
except Exception as e:
logging.error(f"终止程序 {name} 失败: {e}")

del processes[name] # 从进程字典中移除
else:
logging.warning(f"程序 {name} 没有在运行.")

# 启动所有配置中的程序
start_programs(config)

# ...执行其他任务...
time.sleep(10)

# 当你想要停止某个程序时
stop_program("captions_logging.infoer")
stop_program("audio_player")
Loading

0 comments on commit bae0de4

Please sign in to comment.