7
7
from ..exceptions import StopConsumer
8
8
9
9
logger = logging .getLogger ("channels.consumer" )
10
- if not logger .hasHandlers ():
11
- handler = logging .StreamHandler ()
12
- formatter = logging .Formatter (
13
- "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
14
- )
15
- handler .setFormatter (formatter )
16
- logger .addHandler (handler )
17
- logger .setLevel (logging .DEBUG )
10
+ logger .setLevel (logging .DEBUG )
18
11
19
12
20
13
class AsyncHttpConsumer (AsyncConsumer ):
21
14
"""
22
15
Async HTTP consumer. Provides basic primitives for building asynchronous
23
16
HTTP endpoints.
17
+
18
+ Note that the ASGI spec requires that the protocol server only starts
19
+ sending the response to the client after ``self.send_body`` has been
20
+ called the first time.
24
21
"""
25
22
26
23
def __init__ (self , * args , ** kwargs ):
@@ -43,6 +40,10 @@ async def send_headers(self, *, status=200, headers=None):
43
40
async def send_body (self , body , * , more_body = False ):
44
41
"""
45
42
Sends a response body to the client. The method expects a bytestring.
43
+
44
+ Set ``more_body=True`` if you want to send more body content later.
45
+ The default behavior closes the response, and further messages on
46
+ the channel will be ignored.
46
47
"""
47
48
assert isinstance (body , bytes ), "Body is not bytes"
48
49
await self .send (
@@ -51,14 +52,18 @@ async def send_body(self, body, *, more_body=False):
51
52
52
53
async def send_response (self , status , body , ** kwargs ):
53
54
"""
54
- Sends a response to the client.
55
+ Sends a response to the client. This is a thin wrapper over
56
+ ``self.send_headers`` and ``self.send_body``, and everything said
57
+ above applies here as well. This method may only be called once.
55
58
"""
56
59
await self .send_headers (status = status , ** kwargs )
57
60
await self .send_body (body )
58
61
59
62
async def handle (self , body ):
60
63
"""
61
- Receives the request body as a bytestring.
64
+ Receives the request body as a bytestring. Response may be composed
65
+ using the ``self.send*`` methods; the return value of this method is
66
+ thrown away.
62
67
"""
63
68
raise NotImplementedError (
64
69
"Subclasses of AsyncHttpConsumer must provide a handle() method."
@@ -94,10 +99,6 @@ async def http_disconnect(self, message):
94
99
"""
95
100
Let the user do their cleanup and close the consumer.
96
101
"""
97
- try :
98
- await self .disconnect ()
99
- await aclose_old_connections ()
100
- except Exception as e :
101
- logger .error (f"Error during disconnect: { str (e )} " )
102
- finally :
103
- raise StopConsumer ()
102
+ await self .disconnect ()
103
+ await aclose_old_connections ()
104
+ raise StopConsumer ()
0 commit comments