Segment large files for upload without needing tons of RAM#551
Open
Starblade42 wants to merge 1 commit intopycontribs:masterfrom
Open
Segment large files for upload without needing tons of RAM#551Starblade42 wants to merge 1 commit intopycontribs:masterfrom
Starblade42 wants to merge 1 commit intopycontribs:masterfrom
Conversation
drewbrew
reviewed
Jan 28, 2019
|
|
||
| def test_read_in_chunks(self): | ||
| # create junk file to test size. | ||
| source_file = "source_file.dat" |
Collaborator
There was a problem hiding this comment.
Instead of hard-coding the temporary file names, why not use TemporaryFile?
drewbrew
reviewed
Jan 28, 2019
| target.write(chunk) | ||
| compare_contents(source_file, target_file) | ||
| os.unlink(target_file) | ||
| # test max_size equal with chunk size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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._uploadfunction 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.