Propagate close() from DatasetIterator to data sources
#1185
+128
−16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This follows up on #1069 to call
closeon the underlying data source if it implements aclosemethod. This ensures resources are cleaned up properly.For multi-threaded prefetch, the
closemethod is called once per thread and on the main thread to clean up thread-local resources. This means thatcloseshould be safe to call multiple times which aligns with Python standard practice, e.g., closing streams, database connections, and sockets in the standard library have this behavior.The docs mention that Grain tries to use data sources as context managers if they implement the protocol, but that behavior is not currently implemented (cf. #936). The code provides the utility function
use_context_if_available, but it is not used.@iindyk, given our discussion in #1069, I've used
closerather than context managers to clean up resources. This achieves the clean up, but does not address the doc discrepancy in #936. I can add the context manager implementation but wanted to discuss first given the preference forcloseover context managers in #1069.📚 Documentation preview 📚: https://google-grain--1185.org.readthedocs.build/