Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created October 6, 2017 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitsuhiko/6abbeb836106d6e86c7338e100febc8c to your computer and use it in GitHub Desktop.
Save mitsuhiko/6abbeb836106d6e86c7338e100febc8c to your computer and use it in GitHub Desktop.
from threading import Thread
fname = 'testslice'
expected = '\x00\x00\x00\x00a\x00\x00\x00b\x00\x00\x00c\x00\x00\x00d'
slice = 4
open(fname, 'w').close()
def get_slice(idx):
offset = idx * slice
f = open(fname, 'r+')
f.seek(0)
f.seek(offset + slice - 1)
f.write('\x00')
f.seek(0)
f.seek(offset + slice)
return f
def write_byte(idx, b):
with get_slice(idx) as f:
f.write(b)
a = Thread(target=lambda: write_byte(0, 'a'))
b = Thread(target=lambda: write_byte(1, 'b'))
c = Thread(target=lambda: write_byte(2, 'c'))
d = Thread(target=lambda: write_byte(3, 'd'))
a.start()
b.start()
c.start()
d.start()
a.join()
b.join()
c.join()
d.join()
print repr(open(fname).read())
assert open(fname).read() == expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment