SW / SW_run.py
BBrother's picture
Update SW_run.py
0820a4f
raw
history blame
No virus
2.51 kB
import subprocess
import sys
import logging
import os
import binascii # 用于将文本转换为十六进制
# 检查是否存在 logs_run.txt 文件,如果存在则删除它
log_file = '/bushu/logs_run.txt'
if os.path.exists(log_file):
os.remove(log_file)
# 检查是否存在 url.txt 文件,如果存在则删除它
url_file = '/bushu/url.txt'
if os.path.exists(url_file):
os.remove(url_file)
# 配置日志
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 创建一个将输出同时发送到控制台和日志文件的处理程序
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logging.getLogger().addHandler(console_handler)
# 重定向标准输出和标准错误到日志文件和控制台
sys.stdout = logging.getLogger().handlers[0].stream
sys.stderr = logging.getLogger().handlers[0].stream
# 更改当前工作目录为 /bushu/ui
os.chdir('/bushu/ui')
try:
# 在 try 块中运行 /bushu/ui/launch.py,并将输出重定向到日志文件
with open(log_file, 'a') as log_file_handle, open(url_file, 'a') as url_file_handle:
process = subprocess.Popen(['python', '/bushu/ui/launch.py', '--skip-torch-cuda-test', '--share', '--listen', '--xformers', '--enable-insecure-extension-access', '--gradio-queue', '--multiple', '--disable-nan-check', '--no-hashing', '--lowram', '--no-half-vae', '--enable-console-prompts', '--opt-split-attention'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
for line in process.stdout:
# 将所有行转换为十六进制并写入 url.txt
hex_line = binascii.hexlify(line.encode()).decode()
url_file_handle.write(hex_line + '\n')
# 将同样的十六进制行写入 logs_run.txt
log_file_handle.write(hex_line + '\n')
log_file_handle.flush()
process.wait()
except subprocess.CalledProcessError as e:
# 捕获异常并记录到日志文件
logging.exception(f"程序发生异常: {e}")
except Exception as e:
# 捕获其他异常并记录到日志文件
logging.exception(f"程序发生异常: {e}")
# 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
logging.getLogger().removeHandler(console_handler)
console_handler.close()