Re: getting unique id

2022-11-28 Thread Larry Martell
On Wed, Nov 23, 2022 at 3:11 PM Larry Martell  wrote:
>
> On Wed, Nov 23, 2022 at 3:01 PM Thomas Lockhart  
> wrote:
> >
> > Why not use the existing Django AutoField?
>
> Because I have multiple rows with the same batch_id, and also I would
> like the batch_ids to be sequential.
>
> The use case is a batch job dashboard. Users run jobs that spawn many
> sub jobs. The jobs all record their status in the JobStatus table. The
> batch dashboard shows all the sub jobs grouped under their parent job.
> The parent job creates a row with a new batch_id, and that is passed
> to the sub jobs. They all record their status using that same batch
> id, but each in its own row.
>
> > > On Nov 23, 2022, at 8:56 AM, Larry Martell  
> > > 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?

I ended up using another table to get the batch_ids from. It works,
but I still wonder how to make it work without a second table.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACwCsY57mQ%2Bp19gPf21AuhwXi66yatSY10gdSWuGk9s5XZx-HQ%40mail.gmail.com.


Re: getting unique id

2022-11-23 Thread Larry Martell
On Wed, Nov 23, 2022 at 3:01 PM Thomas Lockhart  wrote:
>
> Why not use the existing Django AutoField?

Because I have multiple rows with the same batch_id, and also I would
like the batch_ids to be sequential.

The use case is a batch job dashboard. Users run jobs that spawn many
sub jobs. The jobs all record their status in the JobStatus table. The
batch dashboard shows all the sub jobs grouped under their parent job.
The parent job creates a row with a new batch_id, and that is passed
to the sub jobs. They all record their status using that same batch
id, but each in its own row.

> > On Nov 23, 2022, at 8:56 AM, Larry Martell  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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACwCsY547umxs6E34c4sO7DqSLA1vuOf%2B7FsJDJOGVsR8SmZqw%40mail.gmail.com.


Re: getting unique id

2022-11-23 Thread Larry Martell
On Wed, Nov 23, 2022 at 1:43 PM Shaheed Haque  wrote:
>
> Are all the threads in the same Python process?

No

> Or the same machine?

No

> Do they have to persist across process (or machine) restarts?

Yes.

Playing around with using F() but still don't have it working as desired.

> On Wed, 23 Nov 2022, 16:57 Larry Martell,  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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CACwCsY7A_zk%3DxJ3vg8csCSFPamkjtzm6fRGbzNjru1e2aJfa3A%40mail.gmail.com.


Re: getting unique id

2022-11-23 Thread Thomas Lockhart
Why not use the existing Django AutoField?

- Tom

> On Nov 23, 2022, at 8:56 AM, Larry Martell  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+unsubscr...@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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/59505632-CCFA-4356-86B8-B264528E6A69%40gmail.com.


Re: getting unique id

2022-11-23 Thread Shaheed Haque
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,  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+unsubscr...@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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHAc2jczhpfeAy3khqQiW-LQfKekidQbZs6GqvjDXemi1S8KcQ%40mail.gmail.com.