Wednesday, November 23, 2022

Re: getting unique id

Are all the threads in the same Python process? Or the same machine? Do they have to persist across process (or machine) restarts? 

On Wed, 23 Nov 2022, 16:57 Larry Martell, <larry.martell@gmail.com> wrote:
I have an app that needs to get a unique ID. Many threads run at the
same time that need one. I would like the IDs to be sequential. When I
need a unique ID I do this:

with transaction.atomic():
    max_batch_id =
JobStatus.objects.select_for_update(nowait=False).aggregate(Max('batch_id'))
    json_dict['batch_id'] = max_batch_id['batch_id__max'] + 1
    status_row = JobStatus(**json_dict)
    status_row.save()

But multiple jobs are getting the same ID. Why does the code not work
as I expect? What is a better way to accomplish what I need?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACwCsY54vF6ZK7yy--15hy1u5vxJ%2Bq9Zp_ODGfAX973gPYpecw%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHAc2jczhpfeAy3khqQiW-LQfKekidQbZs6GqvjDXemi1S8KcQ%40mail.gmail.com.

No comments:

Post a Comment