Created
September 7, 2019 10:17
-
-
Save menecio/e01ac908549c54892c53d906761b28ec to your computer and use it in GitHub Desktop.
database_sync_to_async borrows from channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import close_old_connections | |
from asgiref.sync import SyncToAsync | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment