I am trying to put multiple files(ard 25k) into a zip file using multithreading in python cgi. I have written the script below, but somehow the response I get has content length 0 and there is no data in the response. This is my first time using multithreading in python. Is there anything I am missing in the code. Does the output gets printed even before the data is posted?
Any help will be appreciated.
Here is my code:
b = StringIO()
z = zipfile.ZipFile(b, 'w', zipfile.ZIP_DEFLATED)
def read_file(link):
fname = link.split('/')
fname = fname[-1]
z.write(link, fname)
if __name__ == '__main__':
form = cgi.FieldStorage()
fileLinks = form.getvalue("fileLink")
p = Pool(10)
p.map(read_file, fileLinks)
p.close()
p.join()
z.close()
zipFilename = "DataFiles-" + str(time.time()) + ".zip"
length = b.tell()
sys.stdout.write(
HEADERS % ('application/zip', zipFilename, zipFilename, length)
)
b.seek(0)
sys.stdout.write(b.read())
b.close()
Sequential version of the same code:
for fileLink in fileLinks:
fname = fileLink.split('/')
filename = fname[-1]
z.write(fileLink, filename)
z.close()