-
Notifications
You must be signed in to change notification settings - Fork 242
/
core_server.py
75 lines (61 loc) · 2.48 KB
/
core_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import sys
import asyncio
from multiprocessing import Process, Manager
from platform import system
import websockets
from config import ServerConfig as Config
from util.server_cosmic import Cosmic, console
from util.server_check_model import check_model
from util.server_ws_recv import ws_recv
from util.server_ws_send import ws_send
from util.server_init_recognizer import init_recognizer
from util.empty_working_set import empty_current_working_set
BASE_DIR = os.path.dirname(__file__); os.chdir(BASE_DIR) # 确保 os.getcwd() 位置正确,用相对路径加载模型
async def main():
# 检查模型文件
check_model()
console.line(2)
console.rule('[bold #d55252]CapsWriter Offline Server'); console.line()
console.print(f'项目地址:[cyan underline]https://github.com/HaujetZhao/CapsWriter-Offline', end='\n\n')
console.print(f'当前基文件夹:[cyan underline]{BASE_DIR}', end='\n\n')
console.print(f'绑定的服务地址:[cyan underline]{Config.addr}:{Config.port}', end='\n\n')
# 跨进程列表,用于保存 socket 的 id,用于让识别进程查看连接是否中断
Cosmic.sockets_id = Manager().list()
# 负责识别的子进程
recognize_process = Process(target=init_recognizer,
args=(Cosmic.queue_in,
Cosmic.queue_out,
Cosmic.sockets_id),
daemon=True)
recognize_process.start()
Cosmic.queue_out.get()
console.rule('[green3]开始服务')
console.line()
# 清空物理内存工作集
if system() == 'Windows':
empty_current_working_set()
# 负责接收客户端数据的 coroutine
recv = websockets.serve(ws_recv,
Config.addr,
Config.port,
subprotocols=["binary"],
max_size=None)
# 负责发送结果的 coroutine
send = ws_send()
await asyncio.gather(recv, send)
def init():
try:
asyncio.run(main())
except KeyboardInterrupt: # Ctrl-C 停止
console.print('\n再见!')
except OSError as e: # 端口占用
console.print(f'出错了:{e}', style='bright_red'); console.input('...')
except Exception as e:
print(e)
finally:
Cosmic.queue_out.put(None)
sys.exit(0)
# os._exit(0)
if __name__ == "__main__":
init()