Skip to content

Instantly share code, notes, and snippets.

@gaul
Created August 31, 2019 04:08
Show Gist options
  • Save gaul/f72cf4031d2b39b64eac63b2e545b7b5 to your computer and use it in GitHub Desktop.
Save gaul/f72cf4031d2b39b64eac63b2e545b7b5 to your computer and use it in GitHub Desktop.
import os
import threading
NUM_THREADS = 8
FILE_SIZE = 4 * 1024 * 1024 * 1024
BLOCK_SIZE = 1024 * 1024
BLOCK = b'\0' * BLOCK_SIZE
def worker(fd, offset, length):
for i in range(offset, offset + length, BLOCK_SIZE):
os.pwrite(fd, BLOCK, i)
fd = os.open("foo.dat", os.O_CREAT | os.O_TRUNC | os.O_WRONLY)
try:
threads = []
for i in range(NUM_THREADS):
size = FILE_SIZE // NUM_THREADS
thread = threading.Thread(target = worker, args = (fd, i * size, size))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
os.fsync(fd)
finally:
os.close(fd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment