Skip to content

Instantly share code, notes, and snippets.

@ecpost
Created February 5, 2016 19:21
Show Gist options
  • Save ecpost/4fe67024db249e23ec96 to your computer and use it in GitHub Desktop.
Save ecpost/4fe67024db249e23ec96 to your computer and use it in GitHub Desktop.
Demo of Permission denied exception when reading from file while another thread uses werkzeug.posixemulation.rename to replace it.
from werkzeug.posixemulation import rename
from threading import Thread
from time import sleep
filename = 'test_file.dat'
running = True
def create_file():
new_file = 'test_file.tmp'
with open(new_file, 'w') as f:
f.write('abc')
rename(new_file, filename)
def replace_file():
while running:
create_file()
sleep(0.02)
create_file()
thread = Thread(target=replace_file)
thread.start()
while True:
try:
try:
with open(filename) as f:
f.read()
except Exception, ex:
print ex
except KeyboardInterrupt:
running = False
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment