1๐
โ
If you have a lot of data you still may need to load & process it in chunks, try this:
CHUNK_SIZE = 23000
ports_list_save = []
for host in results['hosts']:
for protocol in results['hosts'][host]['protocols']:
for port in results['hosts'][host]['protocols'][protocol]:
current_port = history.Port(number=int(port),
protocol=protocol,
state=results['hosts'][host]['protocols'][protocol][port]['state'],
service='',
version='',
address=history.Ip.objects.get(scan=self.scan, address=host))
ports_list_save.append(current_port)
if len(ports_list_save) > CHUNK_SIZE:
history.Port.objects.bulk_create(ports_list_save)
ports_list_save = []
if ports_list_save:
history.Port.objects.bulk_create(ports_list_save)
๐คmonkut
0๐
I faced with the same problem and ended up with this solution:
class BulkCreateManager(object):
model = None
chunk_size = None
instances = None
def __init__(self, model, chunk_size=None, *args):
self.model = model
self.chunk_size = chunk_size
self.instances = []
def append(self, instance):
if self.chunk_size and len(self.instances) > self.chunk_size:
self.create()
self.instances = []
self.instances.append(instance)
def create(self):
self.model.objects.bulk_create(self.instances)
ports_list_save = BulkCreateManager(history.Port, 23000)
for host in results['hosts']:
for protocol in results['hosts'][host]['protocols']:
for port in results['hosts'][host]['protocols'][protocol]:
current_port = history.Port(number=int(port),
protocol=protocol,
state=results['hosts'][host]['protocols'][protocol][port]['state'],
service='',
version='',
address=history.Ip.objects.get(scan=self.scan, address=host))
ports_list_save.append(current_port)
ports_list_save.create()
๐คramusus
- [Answer]-How to use messages in Django?
- [Answer]-NoReverseMatch exception in a simple Django app
- [Answer]-How to upload multiple images
- [Answer]-How to use "Readonly Field" outside Admin
Source:stackexchange.com