Live data from Hacker News

Writing a chat application in Django 4.2 using async StreamingHttpResponse

valberg.dk

11–20 of 36 posts

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#11
post #3

Just a heads up that currently Django is not cleaning up open PostgreSQL connections when ran in ASGI mode, leading to too many open connections error: https://code.djangoproject.com/ticket/33497

I'll have zero trust running async Django until all sync_to_async and async_to_sync calls are removed from the code base.

[deleted]

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#12

Earlier quoted context omitted.

that’s kind of a dealbreaker

could this be addressed by something like pgbouncer or another connection pooler?

Going off the pooling mode feature map[0], it might be possible to mitigate that issue using transaction pooling, but not session pooling — which means a restricted feature set and cooperation from the application (which would just not call restricted features up).

  [0] http://www.pgbouncer.org/features.html#fn:2

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#13
post #3

Just a heads up that currently Django is not cleaning up open PostgreSQL connections when ran in ASGI mode, leading to too many open connections error: https://code.djangoproject.com/ticket/33497

I'll have zero trust running async Django until all sync_to_async and async_to_sync calls are removed from the code base.

Care to elaborate?

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#14

All the new asyncIO stuff in Django is awesome, they are doing a phenomenal job retrofitting it all to an inherently sync designed framework. One thing to note, it's been possible to build these sort of things with Django by using Gevent and Gunicorn for over a decade, and it works well. In many ways I wish Gevent had been adopted by Python rather than AsyncIO.

Where can I learn more about this? I've been thinking of trying to integrate Supabase Realtime (https://github.com/supabase/realtime) into my Django app (without the rest of Supabase), but I'd also like to keep things even simpler if possible.

Also, what was the reason not to go with Gevent?

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#15

All the new asyncIO stuff in Django is awesome, they are doing a phenomenal job retrofitting it all to an inherently sync designed framework. One thing to note, it's been possible to build these sort of things with Django by using Gevent and Gunicorn for over a decade, and it works well. In many ways I wish Gevent had been adopted by Python rather than AsyncIO.

Can second the Django/Gevent/Gunicorn stack - we use it in production and IMO it's much easier to reason about and code in than asyncio. Just write synchronous Python, including long-running `requests` calls, and the system will yield control whenever you're blocked on network or disk I/O, no matter how deep that is in your stack. The entire ecosystem of Python libraries just works. I also wish more people knew about gevent - it's truly magical, especially if you need to work with APIs with unpredictable latency!

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#16

Earlier quoted context omitted.

I'll have zero trust running async Django until all sync_to_async and async_to_sync calls are removed from the code base.

Care to elaborate?

Look at the intended semantics [1], and then read the implementation [2]. Can you figure out if the implementation is correct? Can you infer the possible limitations of the approach at glance? Can your async library actually handle being called with multiple event loops installed?

I have zero trust in this code and I have been bitten by fixes to this library that introduced deadlocks in my own code.

[1] https://github.com/django/asgiref#synchronous-code--threads.

[2] https://github.com/django/asgiref/blob/main/asgiref/sync.py#...

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#17

All the new asyncIO stuff in Django is awesome, they are doing a phenomenal job retrofitting it all to an inherently sync designed framework. One thing to note, it's been possible to build these sort of things with Django by using Gevent and Gunicorn for over a decade, and it works well. In many ways I wish Gevent had been adopted by Python rather than AsyncIO.

Where can I learn more about this? I've been thinking of trying to integrate Supabase Realtime ( https://github.com/supabase/realtime ) into my Django app (without the rest of Supabase), but I'd also like to keep things even simpler if possible. Also, what was the reason not to go with Gevent?

>Also, what was the reason not to go with Gevent?

The biggest downside of Gevent IMO is that it enables the magic of turning sync code into async code via monkey patching things like the socket lib. This lack of explicitness can make things a bit difficult to reason about, without a good mental model of what Gevent is doing underneath the hood.

Re: Writing a chat application in Django 4.2 using async StreamingHttpResponse

#18

Earlier quoted context omitted.

Care to elaborate?

Look at the intended semantics [1], and then read the implementation [2]. Can you figure out if the implementation is correct? Can you infer the possible limitations of the approach at glance? Can your async library actually handle being called with multiple event loops installed? I have zero trust in this code and I have been bitten by fixes to this library that introduced deadlocks in my own code. [1] https://githu…

Doesn't look like something I'd want to have to debug in a pinch, that's for sure.

Is there anything else in Python-land you would go with for the use case you have in mind? Or does pretty much all the async stuff turn on you like this, at some point or another?

Post reply on HN