Re: Annotate and Subquery with 2 tables in 2 different databases

2023-04-20 Thread Jason
this won't work, you're doing things across two entire dbs.  So you'l have 
to implement all the logic in python to execute.

On Thursday, April 20, 2023 at 9:58:20 AM UTC-4 Mạnh ĐỖ wrote:

> I want to use annotate and subquery to join tables.
> Table1 in database1 and table2 in database2. But it's not work.
> I got the error:
>
>  QuerySet.annotate() received non-expression(s): database1.
>
> the code here:
> from django.db.models import Subquery, OuterRef, Count from app1.models 
> import Table1 from app2.models import Table2 subquery = Table2.objects.
> filter(table1_id=OuterRef('id')).values('table1_id').annotate(count=Count(
> 'id')).values('count') queryset = 
> Table1.objects.annotate(table2_count=Subquery(subquery, 
> output_field=models.IntegerField()), using='database1')
>
> Could anyone help me?
>

-- 
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/99dc653f-ad9e-445a-8d06-310f3351b66dn%40googlegroups.com.


Re: Async views means dropping gunicorn gevent for uvicorn, affecting sync view performance

2023-04-20 Thread awe...@foxmail.com

I think you may split the async endpoints and the sync endpoints into two 
different procress group and start them in different ways( different start 
up command, worker and different port). And config the Load Balancer 
(Nginx?) to point to different ports by path..
I would handle the migration in this way to make sure everything is going 
right in production.
在2023年4月18日星期二 UTC+8 07:14:36 写道:

> I know you can run gunicorn with uvicorn workers. But the sync views will 
> be running sync, no longer gevent patched, meaning performance could 
> change. I’m also unsure if sync views are serialized into one thread if you 
> don’t set DJANGO_ALLOW_ASYNC_UNSAFE, which could be a bottleneck.
>
> I did some more research and thinking and I made two errors in my first 
> post, but it doesn’t really change the matter. 1) doing something like 
> monkey.patch_all() to patch things out with async versions does not work, 
> because you can’t get the patched things to magically somehow be awaited 
> instead of being called normally 2) I didn’t realize you can run django 
> hybrid (both sync and async views) both under WSGI as well as ASGI.
>
> On Monday, April 17, 2023 at 8:34:55 AM UTC+2 TITAS ONLINE MARKET wrote:
>
>> Thanks 
>>
>> On Mon, 17 Apr 2023, 11:42 am Fortune Osho,  wrote:
>>
>>> You can simply run gunicorn with unicorn workers... which is very 
>>> performance using uvloop... test it performance against pure gunicorn... 
>>> note that performance is not totally dependent on server used(a whole lot 
>>> of things can affect performance like moddlewares, serialization etc)
>>>
>>>
>>> On Sun, Apr 16, 2023 at 5:34 PM Chris Barber  
>>> wrote:
>>>
 Hello

 Production django app, currently 100% sync views, running under 
 gunicorn with gevent worker class.

 @andrewgodwin suggests here it is possible to migrate slowly, just run 
 hybrid sync-async and add or convert views as desired
 https://www.youtube.com/watch?v=19Uh_PA_8Rc=1728s

 But to get a single async view one has to switch to uvicorn workers or 
 similar, right? Now one is no longer getting the gevent patching for sync 
 views, which could be a major impact on performance/scaling, and could 
 trigger re-evaluation of the production deployment & tuning!

 Questions:

 1. How to approach this? It has me wondering if monkey.patch_all() 
 should be ported to asyncio and tested under uvicorn. This would smooth 
 the 
 transition since the sync views should perform and scale roughly the same 
 as before.

 2. Also an important technical question on how sync views under ASGI 
 work. By default they will run all in the same thread (*per worker*) 
 which could be really bad for performance with tons of sync views, 
 correct? 
 If I enable DJANGO_ALLOW_ASYNC_UNSAFE, then the sync views will be run in 
 more threads, right? How many threads?Should I expect to have to tune this?

 In general, regardless of the approach, one could presumably A/B test 
 in a live environment - run some workers / nodes under uvicorn, partition 
 the traffic accordingly, and see how it compares. Tune, stress test, and 
 then rolling transition.

 -C

 -- 
 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...@googlegroups.com.
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/django-users/8cc88d44-df9f-4232-8f57-89a1b07fb769n%40googlegroups.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...@googlegroups.com.
>>>
>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-users/CABqs0Mwr%3DXjg6OLUAN9kxsqCKREJW1%2BtrkDHbTq0pnazKgqQkQ%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/03c48517-73e2-4483-b9fe-52f680868ae8n%40googlegroups.com.


Annotate and Subquery with 2 tables in 2 different databases

2023-04-20 Thread Mạnh ĐỖ
I want to use annotate and subquery to join tables.
Table1 in database1 and table2 in database2. But it's not work.
I got the error:

 QuerySet.annotate() received non-expression(s): database1.

the code here:
from django.db.models import Subquery, OuterRef, Count from app1.models 
import Table1 from app2.models import Table2 subquery = Table2.objects.
filter(table1_id=OuterRef('id')).values('table1_id').annotate(count=Count(
'id')).values('count') queryset = 
Table1.objects.annotate(table2_count=Subquery(subquery, 
output_field=models.IntegerField()), using='database1')

Could anyone help me?

-- 
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/5c53820c-ead4-414f-9ecc-0afdc9549e5bn%40googlegroups.com.