Attention
June 2024 Status Update: Removing DataPipes and DataLoader V2
We are re-focusing the torchdata repo to be an iterative enhancement of torch.utils.data.DataLoader. We do not plan on continuing development or maintaining the [DataPipes] and [DataLoaderV2] solutions, and they will be removed from the torchdata repo. We’ll also be revisiting the DataPipes references in pytorch/pytorch. In release torchdata==0.8.0 (July 2024) they will be marked as deprecated, and in 0.9.0 (Oct 2024) they will be deleted. Existing users are advised to pin to torchdata==0.8.0 or an older version until they are able to migrate away. Subsequent releases will not include DataPipes or DataLoaderV2. Please reach out if you suggestions or comments (please use this issue for feedback)
HttpReader¶
- class torchdata.datapipes.iter.HttpReader(source_datapipe: IterDataPipe[str], timeout: Optional[float] = None, skip_on_error: bool = False, **kwargs: Optional[Dict[str, Any]])¶
Takes file URLs (HTTP URLs pointing to files), and yields tuples of file URL and IO stream (functional name:
read_from_http
).- Parameters:
source_datapipe – a DataPipe that contains URLs
timeout – timeout in seconds for HTTP request
skip_on_error – whether to skip over urls causing problems, otherwise an exception is raised
**kwargs – a Dictionary to pass optional arguments that requests takes. For the full list check out https://docs.python-requests.org/en/master/api/
Example:
from torchdata.datapipes.iter import IterableWrapper, HttpReader file_url = "https://raw.githubusercontent.com/pytorch/data/main/LICENSE" query_params = {"auth" : ("fake_username", "fake_password"), "allow_redirects" : True} timeout = 120 http_reader_dp = HttpReader(IterableWrapper([file_url]), timeout=timeout, **query_params) reader_dp = http_reader_dp.readlines() it = iter(reader_dp) path, line = next(it) print((path, line))
Output:
('https://raw.githubusercontent.com/pytorch/data/main/LICENSE', b'BSD 3-Clause License')