Fix ConnectionError in download_mnist (#61789)

Summary:
Fixes issues like the following error. Note that `ConnectionResetError` is a subclass of `ConnectionError`.

```
+ python tools/download_mnist.py --quiet -d test/cpp/api/mnist
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz ...
Traceback (most recent call last):
  File "tools/download_mnist.py", line 93, in <module>
    main()
  File "tools/download_mnist.py", line 86, in main
    download(path, resource, options.quiet)
  File "tools/download_mnist.py", line 42, in download
    urlretrieve(url, destination_path, reporthook=hook)
  File "/opt/conda/lib/python3.6/urllib/request.py", line 277, in urlretrieve
    block = fp.read(bs)
  File "/opt/conda/lib/python3.6/http/client.py", line 463, in read
    n = self.readinto(b)
  File "/opt/conda/lib/python3.6/http/client.py", line 507, in readinto
    n = self.fp.readinto(b)
  File "/opt/conda/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/61789

Reviewed By: dreiss

Differential Revision: D29745459

Pulled By: zhouzhuojie

fbshipit-source-id: 2deb668bd74478f32bd01704d4362e8a4d95087b
This commit is contained in:
zhouzhuojie
2021-07-16 17:00:40 -07:00
committed by Facebook GitHub Bot
parent 4e2fe9718d
commit cb6841b263

View File

@ -40,7 +40,7 @@ def download(destination_path: str, resource: str, quiet: bool) -> None:
try:
hook = None if quiet else report_download_progress
urlretrieve(url, destination_path, reporthook=hook)
except URLError as e:
except (URLError, ConnectionError) as e:
print('Failed to download (trying next):\n{}'.format(e))
continue
finally: