stu1130 commented on a change in pull request #12572: Make Gluon download 
function to be atomic
URL: https://github.com/apache/incubator-mxnet/pull/12572#discussion_r218258922
 
 

 ##########
 File path: python/mxnet/gluon/utils.py
 ##########
 @@ -242,23 +277,34 @@ def download(url, path=None, overwrite=False, 
sha1_hash=None, retries=5, verify_
         dirname = os.path.dirname(os.path.abspath(os.path.expanduser(fname)))
         if not os.path.exists(dirname):
             os.makedirs(dirname)
-        while retries+1 > 0:
+        while retries + 1 > 0:
             # Disable pyling too broad Exception
             # pylint: disable=W0703
             try:
-                print('Downloading %s from %s...'%(fname, url))
+                print('Downloading {} from {}...'.format(fname, url))
                 r = requests.get(url, stream=True, verify=verify_ssl)
                 if r.status_code != 200:
-                    raise RuntimeError("Failed downloading url %s"%url)
-                with open(fname, 'wb') as f:
+                    raise RuntimeError('Failed downloading url {}'.format(url))
+                # create uuid for temporary files
+                random_uuid = str(uuid.uuid4())
+                with open('{}.{}'.format(fname, random_uuid), 'wb') as f:
+                    # create uuid for temporary files
                     for chunk in r.iter_content(chunk_size=1024):
                         if chunk: # filter out keep-alive new chunks
                             f.write(chunk)
+                # if the target file exists(created by other processes),
+                # delete the temporary file
+                if os.path.exists(fname):
+                    os.remove('{}.{}'.format(fname, random_uuid))
 
 Review comment:
   Under multiprocessing environment, two processes might pass the check and 
one of them finish the download, rename. while the other one is still 
downloading or right before rename. In such case, I choose to remove instead of 
rename because there is a rare case where rename might be not atomic in windows 
based on some research on MSDN. it uses remove to mitigate the risk

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to