Difference between sync_to_async vs. database_sync_to_async #2073
-
I understand that database_sync_to_async explicitly closes all DB connections. But when I run sync_to_async with Django 5.0 + Postgres - I don't see a difference in the total number of connections. They seem to have the same effect. Curious what guidance here is? Should I just default to using database_sync_to_async for all cases? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @keithhackbarth,
Django itself calls that for each request when dispatching the request_finised signal. So, if you're running your requests through to Django, likely you'll not need to call it yourself. For requests which go only to Channels consumers, and make use of the database, you likely should use Make sense? |
Beta Was this translation helpful? Give feedback.
Hi @keithhackbarth,
database_sync_to_async()
literally just wrapssync_to_async()
with a call to Django's (internal) clean up functionclose_old_connections()
.Django itself calls that for each request when dispatching the request_finised signal.
So, if you're running your requests through to Django, likely you'll not need to call it yourself. For requests which go only to Channels consumers, and make use of the database, you likely should use
database_sync_to_async()
.Make sense?