Segment large files for upload without needing tons of RAM #551
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.
Reason for the changes and pull request:
This change addresses a bug I found. I'd try to upload a file larger than 5 GB, relying on pyrax to segment and handle the metadata for me, but it gave me a
MemoryError
.Inspecting the code showed that the function was trying to read 5 GB of the file into memory before writing to the disk.
...
This didn't work on my 1 GB cloud server, of course. This should nicely resolve the issue for myself and for others.
The
StorageObjectManager._upload
function will now read and write in small pieces until the segment reaches the desired size, and it seems to work perfectly, especially with the self-deleting tmp file utility function.I only found this issue in the one place, but it's possible it ought to be applied elswhere. If other file segmenting is done on the disk, the new function can be used easily.
Changes made
The function read_in_chunks by default reads in pieces of 8192 bytes (8 Kb) so even memory constrained boxes shouldn't have problems segmenting large files if they have enough disk space. This function required a new function in pyrax.utils
The following changes were made:
Other stuff:
https://github.com/rackspace/pyrax/blob/master/HACKING.rst
Obviously you'll want to verify everything, but I've done what I know to do on my end.
Let me know if I can do anything to help.