Limour commited on
Commit
009cabd
1 Parent(s): 6b70385

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -1,14 +1,31 @@
1
  import subprocess
 
2
  from hf_api import restart_space
3
 
4
  try:
5
  # 启动另一个程序,并通过管道捕获其输出
6
- process = subprocess.Popen(["python", "sub_app.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
7
- while True:
8
- output = process.stdout.readline()
9
- if output:
10
- print(output.decode("utf-8").strip())
11
- if process.poll() is not None:
12
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  finally:
14
  restart_space()
 
1
  import subprocess
2
+ import select
3
  from hf_api import restart_space
4
 
5
  try:
6
  # 启动另一个程序,并通过管道捕获其输出
7
+ process = subprocess.Popen(["python", "sub_app.py"],
8
+ stdout=subprocess.PIPE,
9
+ stderr=subprocess.PIPE,
10
+ bufsize=1, universal_newlines=True)
11
+ while process.poll() is None:
12
+ # 使用 select 模块检查是否有可读数据
13
+ ready_reads, _, _ = select.select([process.stdout, process.stderr], [], [], 1.0)
14
+ for ready in ready_reads:
15
+ # 读取输出并打印
16
+ output = ready.readline()
17
+ if output:
18
+ print(output, end='')
19
+
20
+ # 读取剩余的输出
21
+ for output in process.stdout.readlines() + process.stderr.readlines():
22
+ print(output, end='')
23
+
24
+ # 检查进程的返回代码以确定是否成功结束
25
+ if process.returncode == 0:
26
+ print("Process has terminated successfully.")
27
+ else:
28
+ print(f"Process has terminated with an error. {process.returncode}")
29
+
30
  finally:
31
  restart_space()