H
H
howuu2019-09-19 02:20:39
Django
howuu, 2019-09-19 02:20:39

Multiprocessing django?

I have a loop like this:

name = 'something'
for hash, color in hashes:
        ColorHashes.objects.create(name = name , hash = hash, color = color )

The trouble is that I have about 120 thousand. objects need to be created and the process is still very slow, I want to try to make multithreading, how can this be implemented

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-09-19
@sergey-gornostaev

Try bulk_create

CHUNK_SIZE = 10000  # Подберите наиболее подходящий размер экспериментальным образом

def chunkify(items, chunk_size):
    for i in range(0, len(items), chunk_size):
        yield items[i:i+chunk_size]


name = 'something'
for chunk in chunkify(hashes, CHUNK_SIZE):
    color_hashes = [ColorHashes(name=name , hash=hash, color=color) for hash, color in chunk]
    ColorHashes.objects.bulk_create(color_hashes)

S
sim3x, 2019-09-19
@sim3x

No
You need to disable autocommit in this block of code and let's make a commit in the code after 10k positions
https://docs.djangoproject.com/en/2.2/topics/db/tr...
Or even insert directly through the DBMS console

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question