Skip to content

Commit fa67a9d

Browse files
committed
documented base async middleware
1 parent dd29199 commit fa67a9d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

docs/middleware/base.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## AsyncMiddlewareMixin
2+
3+
a base class to create async only middlewares.
4+
5+
it is very similar to django's [MiddlewareMixin](https://docs.djangoproject.com/en/5.1/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware)
6+
with the following specification:
7+
8+
`__call__` method is async, so you need to `await` the middleware instance.
9+
```pycon
10+
>>> middleware = AsyncMiddlewareMixin(get_response)
11+
>>> await middleware()
12+
```
13+
where `get_response` is an async function, sync functions are not supported and will raise an error.
14+
15+
----------------------------
16+
17+
other methods are as follows:
18+
19+
* `process_request` and `process_response` are `await`ed inside the middleware and have to be async
20+
21+
* `process_view` and `process_template_response` can be either sync or async, but **async is preferred**, if it's sync django will wrap it as async which might have slight performance reduction.
22+
23+
* `process_exception` can be either sync or async, but **sync is preferred**, if async is used django wraps the method to be called synchronously.

0 commit comments

Comments
 (0)