It hasn't failed me yet:

BATCH_SIZE = 100

def batch_proess(queryset) -> None:
    count = queryset.count()
    if count == 0:
        time.sleep(10)
        return
    with transaction.atomic():
        batch = list(
            queryset.select_for_update(of=("self",), skip_locked=True).values_list(
                "id", flat=True
            )[:BATCH_SIZE]
        )
        # Do whatever you need to do with the batch here.
    return

Lightning bolt
Subscribe to my newsletter

I publish monthly roundups of everything I've written, plus pictures of my corgi.
© 2024 Justin Duke · All rights reserved · have a nice day.