Last active
July 19, 2018 21:39
-
-
Save gavinwahl/208cd0d057c234b59d65ec2d54fc2f6e to your computer and use it in GitHub Desktop.
Bulk create your objects, but don't accidentally keep too many in memory
This file contains 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
import contextlib | |
@contextlib.contextmanager | |
def bulk_object_creator(limit=100): | |
""" | |
Allows creation of model objects in bulk, intelligently not using too much memory. | |
with bulk_object_creator() as foo_creator: | |
for i in range(100000): | |
foo_creator.create(Foo(...)) | |
""" | |
creator = _BulkObjectCreator(limit) | |
try: | |
yield creator | |
finally: | |
creator.flush() | |
class _BulkObjectCreator(object): | |
def __init__(self, limit=100): | |
self.pending_objects = [] | |
self.limit = 100 | |
def create(self, obj): | |
assert not self.pending_objects or isinstance(obj, type(self.pending_objects[0])), \ | |
"All objects must be the same type." | |
self.pending_objects.append(obj) | |
if len(self.pending_objects) > self.limit: | |
self.flush() | |
def flush(self): | |
type(self.pending_objects[0]).objects.bulk_create(self.pending_objects) | |
self.pending_objects = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment