Skip to content
Advertisement

Is python zipfile thread-safe?

In a django project, I need to generate some pdf files for objects in db. Since each file takes a few seconds to generate, I use celery to run tasks asynchronously.

Problem is, I need to add each file to a zip archive. I was planning to use the python zipfile module, but different tasks can be run in different threads, and I wonder what will happen if two tasks try to add a file to the archive at the same time.

Is the following code thread safe or not? I cannot find any valuable information in the python’s official doc.

try:
    zippath = os.path.join(pdf_directory, 'archive.zip')
    zipfile = ZipFile(zippath, 'a')
    zipfile.write(pdf_fullname)
finally:
    zipfile.close()

Note: this is running under python 2.6

Advertisement

Answer

No, it is not thread-safe in that sense. If you’re appending to the same zip file, you’d need a lock there, or the file contents could get scrambled. If you’re appending to different zip files, using separate ZipFile() objects, then you’re fine.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement