使用 flask 控制 libreoffice 同时转换多个 docx 文件出错

2019-08-11 22:25:44 +08:00
 Herwifte

现在在开发一个网站,获取客户表单上传的 docx 文件,然后通过 libreoffice 转换成 pdf。 现在测试发现如果多个用户同时上传文件,libreoffice 可以正常转换(在后台已经看到转换后的 pdf 文件),但是总有一个或几个用户返回 FileNotFoundError: [Errno 2] No such file or directory 错误,使用 redis 做消息队列仍然不能解决,求教原因?

相关代码如下:

upload_path = os.path.join(parentdir, 'static/Upload_Files/BeforeSwitchFile/', new_filename)
print_file.save(upload_path)
           
# 将数据添加到列表中
switch = q.enqueue_call(switch_topdf(upload_path))
if switch.func_name == 'success':
	i = new_filename.rindex('.')
    new_filename = new_filename[:i] + '.pdf'
    switched_dir = os.path.join(parentdir, 'static/Upload_Files',
         secure_filename(new_filename))  # 转换 pdf 后的文件路径
    # 读取文件页数
    pageCount = read_pdf_pages(switched_dir)
    cost = pageCount * print_cost +0.2

switch_topdf 函数代码

def switch_topdf(filename):

    cmd = "soffice --headless --convert-to pdf:writer_pdf_Export {} --outdir {}".format(filename, FileSaveDir)  # libreoffice 转换命令
    print(cmd)
    try:
        returnCode = subprocess.call(cmd, shell=True)
        # returnCode = os.system(cmd)
        if returnCode != 0:
            raise IOError("{} failed to switch".format(filename))
    except Exception:
        return 'error'
    else:
        return 'success'
1292 次点击
所在节点    Python
1 条回复
bakabie
2019-08-12 04:39:17 +08:00
会不会是因为 secure_filename 处理后的文件路径找不到的原因?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/591015

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX