ffzeroHua commited on
Commit
6563efb
·
verified ·
1 Parent(s): 1d05c4e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +52 -47
main.py CHANGED
@@ -1,48 +1,53 @@
1
- from fastapi import FastAPI
2
- import subprocess
3
- import asyncio
4
- from contextlib import asynccontextmanager
5
-
6
- # 如果两个机器人进同一个房间,就填两个一样的;如果进不同房间,就改数字
7
- ROOMS = ["4442", "4442"]
8
-
9
- bots = [None, None]
10
-
11
- async def run_bot(index, room_id):
12
- global bots
13
- while True:
14
- print(f"========== 正在启动机器人 {index + 1} (房间 {room_id}) ==========")
15
- # 启动对应的机器人进程,传入房间号
16
- bots[index] = subprocess.Popen(["python", "core_for_test_3.py", room_id])
17
-
18
- # 每秒检查一次该进程是否退出(例如打完一局执行了 exit)
19
- while bots[index].poll() is None:
20
- await asyncio.sleep(1)
21
-
22
- print(f"========== 机器人 {index + 1} 已退出,5秒后自动重连 ==========")
23
- await asyncio.sleep(5)
24
-
25
- @asynccontextmanager
26
- async def lifespan(app: FastAPI):
27
- # 服务启动时,创建两个独立的后台任务运行机器人
28
- tasks = []
29
- for i, room in enumerate(ROOMS):
30
- tasks.append(asyncio.create_task(run_bot(i, room)))
31
- yield
32
- # 容器关闭时,清理这两个进程
33
- for bot in bots:
34
- if bot:
35
- bot.terminate()
36
- for t in tasks:
37
- t.cancel()
38
-
39
- app = FastAPI(lifespan=lifespan)
40
-
41
- # 提供给外部服务定时访问的保活接口
42
- @app.get("/")
43
- def health_check():
44
- status = []
45
- for i, bot in enumerate(bots):
46
- state = "运行中" if bot and bot.poll() is None else "重启中"
47
- status.append(f"Bot {i+1}: {state}")
 
 
 
 
 
48
  return {"status": "alive", "bots": status}
 
1
+ from fastapi import FastAPI
2
+ import subprocess
3
+ import asyncio
4
+ from contextlib import asynccontextmanager
5
+
6
+ # ================= 关键配置 =================
7
+ # 在这里配置两个机器人的房间号和对应的独立账号 ID!
8
+ # 注意:第二个账号必须换成您拥有的另一个有效天凤 ID。
9
+ BOT_CONFIGS = [
10
+ {"room": "4441", "id": "ID215416DE-EJPGCHM8"},
11
+ {"room": "4441", "id": "ID27101B05-nRUZ8Xgg"}
12
+ ]
13
+ # ============================================
14
+
15
+ bots = [None, None]
16
+
17
+ async def run_bot(index, config):
18
+ global bots
19
+ while True:
20
+ room_id = config["room"]
21
+ bot_id = config["id"]
22
+ print(f"========== 正在启动机器人 {index + 1} (房间 {room_id} | 账号 {bot_id}) ==========")
23
+
24
+ # 启动脚本,不仅传房间号,还把专属 ID 传进去
25
+ bots[index] = subprocess.Popen(["python", "-u", "core_for_test_3.py", room_id, bot_id])
26
+
27
+ while bots[index].poll() is None:
28
+ await asyncio.sleep(1)
29
+
30
+ print(f"========== 机器人 {index + 1} 已退出,5秒后自动重连 ==========")
31
+ await asyncio.sleep(5)
32
+
33
+ @asynccontextmanager
34
+ async def lifespan(app: FastAPI):
35
+ tasks = []
36
+ for i, config in enumerate(BOT_CONFIGS):
37
+ tasks.append(asyncio.create_task(run_bot(i, config)))
38
+ yield
39
+ for bot in bots:
40
+ if bot:
41
+ bot.terminate()
42
+ for t in tasks:
43
+ t.cancel()
44
+
45
+ app = FastAPI(lifespan=lifespan)
46
+
47
+ @app.get("/")
48
+ def health_check():
49
+ status = []
50
+ for i, bot in enumerate(bots):
51
+ state = "运行中" if bot and bot.poll() is None else "重启中"
52
+ status.append(f"Bot {i+1}: {state}")
53
  return {"status": "alive", "bots": status}