dlxj commited on
Commit
08196b6
·
1 Parent(s): f9cdae0

优化日志输出

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -32,6 +32,7 @@ initQ = False
32
 
33
  def run_sakurallm():
34
  import subprocess
 
35
 
36
  def run_command(command):
37
  process = subprocess.Popen(
@@ -43,17 +44,22 @@ def run_sakurallm():
43
  universal_newlines=True # 兼容不同平台上的换行符
44
  )
45
 
46
- # 实时打印 stdout 的输出
47
- # for stdout_line in iter(process.stdout.readline, ""):
48
- # print(stdout_line, end="") # 避免多打印一个换行符
 
49
 
50
- # process.stdout.close()
51
- # process.wait()
52
-
53
- # stderr = process.stderr.read()
54
- # process.stderr.close()
 
 
 
 
55
 
56
- # return process.returncode, stderr
57
 
58
  # command = ["ping", "-c", "4", "qq.com"]
59
  command = [
@@ -78,8 +84,8 @@ def run_sakurallm():
78
  '--port',
79
  '8080'
80
  ]
81
- # 不知道为什么 ping 可以打印流式输出, llamacpp server 不能行
82
- # returncode, stderr = run_command(command)
83
  run_command(command)
84
 
85
  # if returncode != 0:
 
32
 
33
  def run_sakurallm():
34
  import subprocess
35
+ import threading
36
 
37
  def run_command(command):
38
  process = subprocess.Popen(
 
44
  universal_newlines=True # 兼容不同平台上的换行符
45
  )
46
 
47
+ def log_stream(stream, prefix):
48
+ for line in iter(stream.readline, ""):
49
+ print(f"{prefix}{line}", end="")
50
+ stream.close()
51
 
52
+ # 使用线程实时打印 stdout 和 stderr,避免阻塞主线程且防止缓冲区填满
53
+ t_stdout = threading.Thread(target=log_stream, args=(process.stdout, "[LLAMA] "))
54
+ t_stderr = threading.Thread(target=log_stream, args=(process.stderr, "[LLAMA_ERR] "))
55
+
56
+ t_stdout.daemon = True
57
+ t_stderr.daemon = True
58
+
59
+ t_stdout.start()
60
+ t_stderr.start()
61
 
62
+ return process
63
 
64
  # command = ["ping", "-c", "4", "qq.com"]
65
  command = [
 
84
  '--port',
85
  '8080'
86
  ]
87
+
88
+ print("Starting SakuraLLM server...")
89
  run_command(command)
90
 
91
  # if returncode != 0: