summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2022-12-13 16:15:25 +0100
committerCarlton Gibson <carlton.gibson@noumenal.es>2022-12-22 10:41:12 +0100
commit0bd2c0c9015b53c41394a1c0989afbfd94dc2830 (patch)
tree6b24758335cf10eeedfdf7dec50cda3500796305 /django/utils
parentae0899be0d787fbfc5f5ab2b18c5a8219d822d2b (diff)
Fixed #33735 -- Added async support to StreamingHttpResponse.
Thanks to Florian Vazelle for initial exploratory work, and to Nick Pope and Mariusz Felisiak for review.
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/asyncio.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/django/utils/asyncio.py b/django/utils/asyncio.py
index 1e79f90c2c..eea2df48e2 100644
--- a/django/utils/asyncio.py
+++ b/django/utils/asyncio.py
@@ -37,3 +37,28 @@ def async_unsafe(message):
return decorator(func)
else:
return decorator
+
+
+try:
+ from contextlib import aclosing
+except ImportError:
+ # TODO: Remove when dropping support for PY39.
+ from contextlib import AbstractAsyncContextManager
+
+ # Backport of contextlib.aclosing() from Python 3.10. Copyright (C) Python
+ # Software Foundation (see LICENSE.python).
+ class aclosing(AbstractAsyncContextManager):
+ """
+ Async context manager for safely finalizing an asynchronously
+ cleaned-up resource such as an async generator, calling its
+ ``aclose()`` method.
+ """
+
+ def __init__(self, thing):
+ self.thing = thing
+
+ async def __aenter__(self):
+ return self.thing
+
+ async def __aexit__(self, *exc_info):
+ await self.thing.aclose()