So I have a web server which gets a request for some file, and if the file does not exist, it downloads it from a remote server, and then serve it ie:
The main problem with this (which I wasn't expecting) occurs when several requests for resources in the same non-existing folder arrive simultanously, which means several folder creation commands for the same folder, which makes my system unhappy. To be exact, I get the following error code:
WindowsError: [Error 183] Cannot create a file when that file already exists
I am assuming putting some sort of a lock before creating a folder is the way to go, but I have no idea where to start.
Any tips/pointers/full code solutions would be greatly appreciated.
Thanks,
Itamar
Code:
def do_GET(self):
try:
if not os.path.exists(self.localpath + self.path):#no such file
if not os.path.exists(self.localpath + self.path[:self.path.rfind('/') + 1]):#no such directory
os.makedirs(self.path[:self.path.rfind('/') + 1])
WindowsError: [Error 183] Cannot create a file when that file already exists
I am assuming putting some sort of a lock before creating a folder is the way to go, but I have no idea where to start.
Any tips/pointers/full code solutions would be greatly appreciated.
Thanks,
Itamar
Comment