You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16 lines
469 B
Python

from starlette.types import ASGIApp, Receive, Scope, Send
from django.db import close_old_connections, reset_queries
class DjangoDbConnectionCleanupMiddleware:
def __init__(self, app: ASGIApp):
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send):
reset_queries()
close_old_connections()
try:
await self.app(scope, receive, send)
finally:
close_old_connections()