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

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

现在在开发一个网站,获取客户表单上传的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'

以上是 使用 flask 控制 libreoffice 同时转换多个 docx 文件出错 的全部内容, 来源链接: utcz.com/a/163797.html

回到顶部