BBrother commited on
Commit
12aa729
1 Parent(s): da0dfb4

Update SW_run.py

Browse files
Files changed (1) hide show
  1. SW_run.py +31 -25
SW_run.py CHANGED
@@ -1,32 +1,38 @@
1
  import subprocess
 
2
  import logging
3
- import re
4
 
5
- # 配置日志
6
- logging.basicConfig(filename='logs_run.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
 
7
 
8
- # 执行第二个命令,并捕获输出
9
- proc = subprocess.Popen(['python', '/content/ui/launch.py', '--listen', '--xformers', '--enable-insecure-extension-access',
10
- '--theme', 'dark', '--gradio-queue', '--no-download-sd-model', '--share'],
11
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
12
 
13
- # 读取输出并捕获地址
14
- while True:
15
- output = proc.stdout.readline().strip()
16
- if not output:
17
- break
18
-
19
- # 记录输出到日志
20
- logging.info(output)
21
-
22
- # 查找 "Public WebUI Colab URL" 字符串
23
- match = re.search(r'Public WebUI Colab URL: (.+)', output)
24
- if match:
25
- url = match.group(1)
26
- print(url) # 打印地址到控制台
27
 
28
- # 等待程序执行完成
29
- proc.wait()
 
30
 
31
- # 关闭日志处理程序
32
- logging.shutdown()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import subprocess
2
+ import sys
3
  import logging
4
+ import os
5
 
6
+ # 检查是否存在 logs.txt 文件,如果存在则删除它
7
+ log_file = '/content/logs.txt'
8
+ if os.path.exists(log_file):
9
+ os.remove(log_file)
10
 
11
+ # 配置日志
12
+ logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
 
13
 
14
+ # 创建一个将输出同时发送到控制台和日志文件的处理程序
15
+ console_handler = logging.StreamHandler(sys.stdout)
16
+ console_handler.setLevel(logging.INFO)
17
+ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
18
+ console_handler.setFormatter(formatter)
19
+ logging.getLogger().addHandler(console_handler)
 
 
 
 
 
 
 
 
20
 
21
+ # 重定向标准输出和标准错误到日志文件和控制台
22
+ sys.stdout = logging.getLogger().handlers[0].stream
23
+ sys.stderr = logging.getLogger().handlers[0].stream
24
 
25
+ try:
26
+ # 在try块中运行v2_3.py,并将输出重定向到日志文件
27
+ with open(log_file, 'a') as log_file_handle:
28
+ subprocess.run(['python', 'v2_3.py'], check=True, stdout=log_file_handle, stderr=subprocess.STDOUT)
29
+ except subprocess.CalledProcessError as e:
30
+ # 捕获异常并记录到日志文件
31
+ logging.exception(f"程序发生异常: {e}")
32
+ except Exception as e:
33
+ # 捕获其他异常并记录到日志文件
34
+ logging.exception(f"程序发生异常: {e}")
35
+ finally:
36
+ # 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
37
+ logging.getLogger().removeHandler(console_handler)
38
+ console_handler.close()