-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathstartup.py
More file actions
288 lines (238 loc) · 7.96 KB
/
Copy pathstartup.py
File metadata and controls
288 lines (238 loc) · 7.96 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import asyncio
import logging
import logging.config
import multiprocessing
import multiprocessing as mp
import os
import sys
from contextlib import asynccontextmanager
from multiprocessing import Process
from config.config import Configs
from server.server import create_app
from utils.log_common import build_logger, get_timestamp_ms, get_config_dict, get_log_file
try:
import numexpr
n_cores = numexpr.utils.detect_number_of_cores()
os.environ["NUMEXPR_MAX_THREADS"] = str(n_cores)
except:
pass
import click
from fastapi import FastAPI
logger = build_logger()
def _set_app_event(app: FastAPI, started_event: mp.Event = None):
@asynccontextmanager
async def lifespan(app: FastAPI):
if started_event is not None:
started_event.set()
yield
app.router.lifespan_context = lifespan
def run_api_server(
started_event: mp.Event = None
):
import uvicorn
app = create_app()
_set_app_event(app, started_event)
host = Configs.basic_config.api_server["host"]
port = Configs.basic_config.api_server["port"]
logging_conf = get_config_dict(
"INFO",
get_log_file(log_path=Configs.basic_config.LOG_PATH, sub_dir=f"run_api_server_{get_timestamp_ms()}"),
1024 * 1024 * 1024 * 3,
1024 * 1024 * 1024 * 3,
)
logging.config.dictConfig(logging_conf) # type: ignore
uvicorn.run(app, host=host, port=port)
def run_webui(
started_event: mp.Event = None
):
host = Configs.basic_config.webui_server["host"]
port = Configs.basic_config.webui_server["port"]
script_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "web/webui.py")
flag_options = {
"server_address": host,
"server_port": port,
"theme_base": "light",
"theme_primaryColor": "#165dff",
"theme_secondaryBackgroundColor": "#f5f5f5",
"theme_textColor": "#000000",
"global_disableWatchdogWarning": None,
"global_disableWidgetStateDuplicationWarning": None,
"global_showWarningOnDirectExecution": None,
"global_developmentMode": None,
"global_logLevel": None,
"global_unitTest": None,
"global_suppressDeprecationWarnings": None,
"global_minCachedMessageSize": None,
"global_maxCachedMessageAge": None,
"global_storeCachedForwardMessagesInMemory": None,
"global_dataFrameSerialization": None,
"logger_level": None,
"logger_messageFormat": None,
"logger_enableRich": None,
"client_caching": None,
"client_displayEnabled": None,
"client_showErrorDetails": None,
"client_toolbarMode": None,
"client_showSidebarNavigation": None,
"runner_magicEnabled": None,
"runner_installTracer": None,
"runner_fixMatplotlib": None,
"runner_postScriptGC": None,
"runner_fastReruns": None,
"runner_enforceSerializableSessionState": None,
"runner_enumCoercion": None,
"server_folderWatchBlacklist": None,
"server_fileWatcherType": "none",
"server_headless": None,
"server_runOnSave": None,
"server_allowRunOnSave": None,
"server_scriptHealthCheckEnabled": None,
"server_baseUrlPath": None,
"server_enableCORS": None,
"server_enableXsrfProtection": None,
"server_maxUploadSize": None,
"server_maxMessageSize": None,
"server_enableArrowTruncation": None,
"server_enableWebsocketCompression": None,
"server_enableStaticServing": None,
"browser_serverAddress": None,
"browser_gatherUsageStats": None,
"browser_serverPort": None,
"server_sslCertFile": None,
"server_sslKeyFile": None,
"ui_hideTopBar": None,
"ui_hideSidebarNav": None,
"magic_displayRootDocString": None,
"magic_displayLastExprIfNoSemicolon": None,
"deprecation_showfileUploaderEncoding": None,
"deprecation_showImageFormat": None,
"deprecation_showPyplotGlobalUse": None,
"theme_backgroundColor": None,
"theme_font": None,
}
args = []
try:
# for streamlit >= 1.12.1
from streamlit.web import bootstrap
except ImportError:
from streamlit import bootstrap
logging_conf = get_config_dict(
"INFO",
get_log_file(log_path=Configs.basic_config.LOG_PATH, sub_dir=f"run_webui_{get_timestamp_ms()}"),
1024 * 1024 * 1024 * 3,
1024 * 1024 * 1024 * 3,
)
logging.config.dictConfig(logging_conf) # type: ignore
bootstrap.load_config_options(flag_options=flag_options)
bootstrap.run(script_dir, False, args, flag_options)
started_event.set()
async def start_main_server(args):
import signal
def handler(signalname):
"""
Python 3.9 has `signal.strsignal(signalnum)` so this closure would not be needed.
Also, 3.8 includes `signal.valid_signals()` that can be used to create a mapping for the same purpose.
"""
def f(signal_received, frame):
raise KeyboardInterrupt(f"{signalname} received")
return f
# This will be inherited by the child process if it is forked (not spawned)
signal.signal(signal.SIGINT, handler("SIGINT"))
signal.signal(signal.SIGTERM, handler("SIGTERM"))
mp.set_start_method("spawn")
manager = mp.Manager()
if args.all:
args.api = True
args.webui = True
if len(sys.argv) > 1:
logger.info(f"Starting service:")
processes = {}
api_started = manager.Event()
if args.api:
process = Process(
target=run_api_server,
name=f"API Server",
kwargs=dict(
started_event=api_started
),
daemon=False,
)
processes["api"] = process
webui_started = manager.Event()
if args.webui:
process = Process(
target=run_webui,
name=f"WEBUI Server",
kwargs=dict(
started_event=webui_started
),
daemon=True,
)
processes["webui"] = process
try:
if p := processes.get("api"):
p.start()
p.name = f"{p.name} ({p.pid})"
api_started.wait() # 等待api.py启动完成
if p := processes.get("webui"):
p.start()
p.name = f"{p.name} ({p.pid})"
webui_started.wait() # 等待webui.py启动完成
# 等待所有进程退出
while processes:
for p in processes.values():
p.join(2)
if not p.is_alive():
processes.pop(p.name)
except Exception as e:
logger.error(e)
logger.warning("Caught KeyboardInterrupt! Setting stop event...")
finally:
for p in processes.values():
logger.warning("Sending SIGKILL to %s", p)
# Queues and other inter-process communication primitives can break when
# process is killed, but we don't care here
if isinstance(p, dict):
for process in p.values():
process.kill()
else:
p.kill()
for p in processes.values():
logger.info("Process status: %s", p)
@click.command(help="RAG")
@click.option(
"-a",
"--all",
"all",
is_flag=True,
help="run api.py and webui.py",
)
@click.option(
"--api",
"api",
is_flag=True,
help="run api.py",
)
@click.option(
"-w",
"--webui",
"webui",
is_flag=True,
help="run webui.py server",
)
def main(all, api, webui):
class args:
...
args.all = all
args.api = api
args.webui = webui
multiprocessing.freeze_support()
if sys.version_info < (3, 10):
loop = asyncio.get_event_loop()
else:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(start_main_server(args))