cnks 0.2.5__py3-none-any.whl → 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
src/main.py ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ CNKS主入口模块
6
+
7
+ 启动服务器组件。在新的通信模式下,worker将被server按需调用,不再作为独立进程运行。
8
+ """
9
+
10
+ import signal
11
+ import sys
12
+ import logging
13
+ import asyncio
14
+
15
+ # 配置日志
16
+ logging.basicConfig(
17
+ level=logging.INFO,
18
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
19
+ filename="cnks_main.log",
20
+ filemode="a"
21
+ )
22
+ logger = logging.getLogger("cnks.main")
23
+
24
+ def signal_handler(sig, frame):
25
+ """处理终止信号"""
26
+ logger.info(f"接收到信号 {sig},正在停止系统...")
27
+ print(f"接收到信号 {sig},正在停止系统...")
28
+ sys.exit(0)
29
+
30
+ def main():
31
+ """
32
+ 主入口函数,启动服务器
33
+
34
+ 在新的设计中,服务器会按需调用worker的API,
35
+ 不再需要单独的worker进程。
36
+ """
37
+ # 捕获终止信号
38
+ signal.signal(signal.SIGINT, signal_handler)
39
+ signal.signal(signal.SIGTERM, signal_handler)
40
+
41
+ try:
42
+ logger.info("正在启动CNKS系统...")
43
+ print("正在启动CNKS系统...")
44
+
45
+ # 导入服务器模块
46
+ try:
47
+ from src.server import main as server_main
48
+ except ImportError:
49
+ # 尝试替代导入路径
50
+ from server import main as server_main
51
+
52
+ logger.info("服务器导入成功,正在启动...")
53
+
54
+ # 启动服务器 - 使用asyncio.run运行异步函数
55
+ asyncio.run(server_main())
56
+
57
+ except KeyboardInterrupt:
58
+ logger.info("接收到键盘中断,正在停止系统...")
59
+ print("\n接收到键盘中断,正在停止系统...")
60
+ except Exception as e:
61
+ logger.error(f"系统错误: {str(e)}")
62
+ print(f"系统错误: {str(e)}")
63
+ import traceback
64
+ logger.error(traceback.format_exc())
65
+ finally:
66
+ logger.info("系统已停止")
67
+ print("系统已停止")
68
+
69
+ if __name__ == "__main__":
70
+ main()