File: sync.py

package info (click to toggle)
python-django-debug-toolbar 1%3A3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,668 kB
  • sloc: python: 5,129; javascript: 604; makefile: 55; sh: 1
file content (22 lines) | stat: -rw-r--r-- 593 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
Taken from channels.db
"""
from asgiref.sync import SyncToAsync
from django.db import close_old_connections


class DatabaseSyncToAsync(SyncToAsync):
    """
    SyncToAsync version that cleans up old database connections when it exits.
    """

    def thread_handler(self, loop, *args, **kwargs):
        close_old_connections()
        try:
            return super().thread_handler(loop, *args, **kwargs)
        finally:
            close_old_connections()


# The class is TitleCased, but we want to encourage use as a callable/decorator
database_sync_to_async = DatabaseSyncToAsync