Base
AsyncMiddlewareMixin
a base class to create async only middlewares.
it is very similar to django's MiddlewareMixin with the following specification:
__call__ method is async, so you need to await the middleware instance.
>>> middleware = AsyncMiddlewareMixin(get_response)
>>> await middleware()
where get_response is an async function, sync functions are not supported and will raise an error.
Note: you can use middlewares drove from this base class with normal django middlewares, you can even write sync views
get_response is usually provided by django, so you don't have to worry about it being async.
other methods are as follows:
-
process_requestandprocess_responseareawaited inside the middleware and have to be async. -
process_viewandprocess_template_responsecan 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. -
process_exceptioncan be either sync or async, but sync is preferred, if async is used django wraps the method to be called synchronously.