Skip to content

Instantly share code, notes, and snippets.

@gavinwahl
Last active July 19, 2018 21:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavinwahl/208cd0d057c234b59d65ec2d54fc2f6e to your computer and use it in GitHub Desktop.
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
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