Created
February 5, 2016 19:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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