Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
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"],
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|