我有一个 Python Flask 服务器,它在一条路线上提供 doc 文件,将它们保存到特定于作业的文件夹中,然后当所有.docx
文件上传时,一条新路线会触发一个批处理、多线程作业以将它们转换为 PDF。
问题是如果我从同一个客户端发送第二个请求以执行不同的作业,第一个作业可以正常完成,但第二个和后续处理第一个和第二个作业中请求的所有文件并将它们复制到第二个或后续输出文件夹。
路线:
@app.route('/docxgroupproc2',methods=['GET'])
def docgroupproc2():
startTime=datetime.now()
jobid=request.args.get('jobid')
newPath, jobPath, outPath=paths('',str(jobid))
localdata=local()
localdata2=local()
localdata.value=jobPath
localdata2.value=outPath
mtThread= Thread(target=mtconvDOCX.bleck, args=(localdata.value,localdata2.value),daemon=True)
mtThread.setDaemon(True)
mtThread.start()
print("Thread Started")
mtThread.join()
endTime=datetime.now()
print(endTime-startTime)
return ({'completed': "status"})
多线程模块:
def parseDOCS(outPath,file):
comtypes.CoInitialize()
word=comtypes.client.CreateObject('Word.Application')
word.Visible=False
doc= word.Documents.Open(file,Visible=False)
outFile = os.path.join(outPath,str(os.path.splitext(os.path.basename(file))[0] + ".PDF"))
try:
doc.SaveAs(outFile, FileFormat=wdFormatPDF)
except COMError:
res = "FAIL"
else:
res = "SUCCESS"
finally:
doc.Close()
word.quit()
return
def setupParse(dir,fileCounter=0,TotalFileCounter=0,fileslist=[]):
"return number of files in dir"
for files in os.scandir(dir):
if files.is_file():
fileCounter+=1
TotalFileCounter=+1
fileslist.append(files.path)
text="DOCX Files" + " : " + str(fileCounter) + "\ntotal files: " + str(TotalFileCounter)
print(fileCounter)
#dictlist=map([(x,outPath) for x in [fileslist]])
return text, fileCounter, TotalFileCounter, fileslist
def bleck(dir, outPath):
text, fileCounter, TotalFileCounter, dictlist=setupParse(dir)
pool=ThreadPool(4)
#result=pool.starmap_async(parseDOCS,zip(dictlist, repeat(outPath)),chunksize=1)
result=pool.map_async(partial(parseDOCS,outPath),dictlist)
while not result.ready():
print("\rNumber of Files Processed: {}".format(fileCounter-result._number_left+1), end=' ')
pass
pool.close()
pool.join()
return "completed"