Skip to content

Instantly share code, notes, and snippets.

Created May 7, 2021 15:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Python and threading don't mix
# Write to a tarball from multiple threads to demonstrate corruption.
import tarfile, threading, io
def append_tar(tarf, val):
tf = tarfile.TarInfo(name="{}".format(val))
# Generate an entry with the unique byte
# repeated so it's easy to see intermixed content.
tf.size = 4096
buf = bytearray([val for x in range(tf.size)])
tarf.addfile(tf, io.BytesIO(buf))
tarf ="out.tar", mode='w')
threads = []
for x in range(N_THREADS):
t = threading.Thread(target=lambda: append_tar(tarf, x))
for t in threads:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment