使用同步写入,并且加上文件验证,文件延时0.5秒
This commit is contained in:
@@ -155,14 +155,39 @@ async def md_str_to_html(md_content, output_html):
|
|||||||
</style>
|
</style>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 异步写入 HTML 文件
|
# 构建完整的 HTML 内容
|
||||||
async with aiofiles.open(output_html, 'w', encoding='utf-8') as f:
|
full_html = f'''<html>
|
||||||
await f.write('<html><head>')
|
<head>
|
||||||
await f.write('<meta charset="UTF-8">') # 确保 UTF-8 编码
|
<meta charset="UTF-8">
|
||||||
await f.write(css)
|
{css}
|
||||||
await f.write('</head><body>')
|
</head>
|
||||||
await f.write(html_content)
|
<body>
|
||||||
await f.write('</body></html>')
|
{html_content}
|
||||||
|
</body>
|
||||||
|
</html>'''
|
||||||
|
|
||||||
|
# 使用普通的文件写入,确保文件完全写入
|
||||||
|
try:
|
||||||
|
with open(output_html, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(full_html)
|
||||||
|
f.flush() # 强制刷新缓冲区
|
||||||
|
os.fsync(f.fileno()) # 确保写入磁盘
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"写入HTML文件失败: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
# 验证文件是否成功写入
|
||||||
|
try:
|
||||||
|
with open(output_html, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
if not content:
|
||||||
|
raise ValueError("HTML文件写入后为空")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"验证HTML文件失败: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
|
# 添加小延时确保文件系统同步
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
|
|
||||||
def check_chromium_installed(path):
|
def check_chromium_installed(path):
|
||||||
|
|||||||
Reference in New Issue
Block a user